What is the best way to establish a Telnet connection in PHP using fsockopen?

Establishing a Telnet connection in PHP using fsockopen involves specifying the Telnet server's hostname or IP address and port number, then opening a socket connection to that server. Once the connection is established, you can send commands and receive responses using the socket connection.

$telnetServer = 'example.com';
$telnetPort = 23;

$socket = fsockopen($telnetServer, $telnetPort, $errno, $errstr, 10);
if (!$socket) {
    die("Failed to connect to Telnet server: $errstr ($errno)");
}

// Send commands and receive responses using $socket

fclose($socket);