Try this:
if (! $sock = @fsockopen('localserver', 80, $num, $error, 3))
header("location: http://remoteserver/index.php");
else {
fclose($sock);
header("location: http://sub.localserver/");
}
Note that the '3' (last parameter) in [man]fsockopen/man is the timeout parameter. If it is a local server, it should respond fairly quickly, so I specified a shorter timeout. You may need to adjust this, however.