Are there any best practices for handling errors and exceptions when using fsockopen in PHP?

When using fsockopen in PHP, it is important to handle errors and exceptions properly to ensure robust error management. One best practice is to check for errors using the function `error_get_last()` after calling `fsockopen` to see if any errors occurred during the connection attempt. Additionally, it is recommended to use try-catch blocks to catch exceptions that may be thrown during the socket connection process.

$socket = fsockopen('example.com', 80, $errno, $errstr, 30);
if (!$socket) {
    $error = error_get_last();
    if ($error) {
        echo "Error: " . $error['message'];
    }
} else {
    // Socket connection successful, continue with your code
    fclose($socket);
}