use strict; use warnings; use Socket; use Socket6; my @res = getaddrinfo($D{'XXTarget'}, $D{'XXPort'}, AF_UNSPEC, SOCK_STREAM); my $sock; my $host; my $port; while (@res >= 5) { my ($family, $socktype, $proto, $saddr, $canonname) = splice(@res, 0, 5); ($host, $port) = getnameinfo($saddr, NI_NUMERICHOST | NI_NUMERICSERV); # log("Trying to connect to $host port port $port...\n"); socket($sock, $family, $socktype, $proto) or next; connect($sock, $saddr) and last; undef $sock; } if (not defined $sock) { ... Handle error ... } ... $sock is connected. $host and $post are available ...