What are some common pitfalls when using fsockopen in PHP to access external servers like ICQ?

One common pitfall when using fsockopen in PHP to access external servers like ICQ is not properly handling errors or timeouts, which can lead to unexpected behavior or performance issues. To solve this, make sure to check for errors and set appropriate timeout values when opening the socket connection.

$socket = fsockopen('icq.com', 443, $errno, $errstr, 5);
if (!$socket) {
    echo "Error: $errstr ($errno)";
} else {
    stream_set_timeout($socket, 5);
    // Perform actions with the socket connection
    fclose($socket);
}