use IO::Socket; my @ips = ('XXX.XXX.XXX.X0','XXX.XXX.XXX.X1','XXX.XXX.XXX.X2'); my $port = XXXX; my $sock; while (@ips) { # radmomly pick an ip from the list (removing it at the same time) my $ip = splice(@ips, rand(@ips), 1); # try to connect $sock = IO::Socket::INET->new( PeerAddr => $ip.':'.$port, Blocking => 1, Timeout => 1, ); # success! last if ($sock); }