... create sockets ... my @fileno = (fileno(SOCKET1), fileno(SOCKET2), fileno(SOCKET3), fileno(SOCKET4)); my ($rbits, $ebits); vec($rbits, $_, 1) = 1 for @fileno; vec($ebits, $_, 1) = 1 for @fileno; while (1) { my $time_to_next_heartbeat = ...; # How long till we have to send a hb if ($time_to_next_heartbeat <= 0) { ... send heartbeats ... redo; } if (select (my $rbits = $rbits, undef, my $ebits = $ebits, $time_to_next_heartbeat)) { if ($ebits) { ... deal with errors ... } if ($rbits) { foreach my $fileno (@fileno) { if (vec($rbits, $filen0, 1)) { ... read data ... } } } } }