<?php

#
# An attempt to enable asynchronical calling
# applying the stream_set_blocking feature
#
function doRequestAsync(&$fp$host$path '/'$port 80$timeout 5) {
    if (!
$fp) {
        if (!
$fp = @fsockopen($host$port$errno$errmsg$timeout)) {
            return 
false;
        }
        print 
"Connected...\n";

        
// Disable blocking
        
stream_set_blocking($fp0);
        print 
"Putting request...\n";

        
// Put request
        
fputs($fp"GET $path HTTP/1.0\r\n");
        
fputs($fp"Host: $host\r\n");
        
fputs($fp"\r\n");
    }

    if (
is_resource($fp) && !feof($fp)) {
        return 
fgets($fp1024);
    } else if (
is_resource($fp) && feof($fp)) {
        print 
"Nothing to do; closing up...\n";
        
fclose($fp);
        return 
false;
    }
}

$content doRequestAsync($fp'www.php.net');
$content2 doRequestAsync($fp2'www.php.net');

while ((
is_resource($fp) && !feof($fp)) ||
       (
is_resource($fp2) && !feof($fp2))) {
   if (
is_resource($fp) && !feof($fp)) {
       print 
"Retrieving from Socket #1...\n";
       
$content .= doRequestAsync($fp'www.php.net');
   }
   if (
is_resource($fp) && !feof($fp2)) {
       print 
"Retrieving from Socket #2...\n";
       
$content2 .= doRequestAsync($fp2'www.php.net');
   }
}

echo 
'Received from Stream #1: 'strlen($content), " bytes;\n";
echo 
'Received from Stream #2: 'strlen($content2), " bytes.\n";

?>