while ( 1 ) { unless ($sock->connected) { $sock->connect($port, $ip) or die $!; } # ... }