Wait, are you saying the public IP address isn't the one the server is at? that's screwy... behind a home router or something?
If you are accepting requests by IP address, you could always pull it from REQUEST_URI instead.
$domainPath should then be either the IP address OR the domain name depending on the request type . If you NEED the IP address, test if it's a valid IP addy (another explode and four is_numeric?), if it isn't, then call gethostbyname.