I'm trying to use fsockopen, fwrite, and fread to process some data from a back end system. To cover my bases, I'd like to put some error coding in there should the server not be available. For some reason, even if the server isn't available, the socket is created, data written to it, but when the response is read, the code seizes. I could put an "or die" statement in there, but I want to act on the error, not just output the error to the page. What's the best way to capture this error? Here's a snippet of my code:

PHP Code:
$socket fsockopen($server,$port,$errno,$error5);

$buf "";

$buf fread($socket,1);

If the server is unavailable, then we never get to the echo "4" statement.. I would have thought that the error would have been caught at the socket check, but it makes it all the way to the fread. This has another problem, the fread takes forever because the server doesn't exist, so the page takes awhile to return anything. The earlier I know that the socket is invalid, the better. Is there another way to know if the socket is invalid before getting to the fwrite and fread?