use strict; use warnings; use Fcntl; use Errno; use Socket; use IO::Select; my $socket; my $flags; my $port = 80; my $ip = '1.1.1.1'; # create socket socket($socket, PF_INET, SOCK_STREAM, getprotobyname('tcp')) || die("socket: $!"); # set the socket to non-blocking mode $flags = fcntl($socket, F_GETFL, $flags) || die("fcntl: $!"); fcntl($socket, F_SETFL, $flags | O_NONBLOCK) || die("fcntl: $!"); if (! connect($socket, sockaddr_in($port, inet_aton($ip))) { if ($!{EINPROGRESS}) { ... run a select loop or do something else here installing a callback handler is a good bet ... return; } else { die("connect: $!"); } } else { handle_successful_connect(); } sub callback_handler { my $socket = shift; # check the error condition on the socket my $option = getsockopt($socket, SOL_SOCKET, SO_ERROR); if (! defined($option)) { die("getsockopt: $!"); } if (0 != ($! = unpack('i', $option))) { die("connect: $!"); } handle_successful_connect(); } sub handle_successful_connect { }