This is that universal timeout functionality you dreamed about and always wanted to have and guess what - it's as reliable as it gets, it's basically bulletproof. It can interrupt absolutely anything you throw at it and more, you name it - socket_connect(), socket_read(), fread(), infinite while() loops, sleep(), semaphores - seriously, any blocking operation. You can specify your own handler and just get over anything that normally would make your code unresponsive.
<?php
pcntl_async_signals(TRUE);
$timed_out = FALSE;
pcntl_signal(SIGALRM, function($signal) use (&$timed_out) {
$timed_out = TRUE;
});
pcntl_alarm(2);
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
$connection = socket_connect($socket, 'irc.ircnet.com', 6667);
$timed_out || pcntl_alarm(0);
$status = $connection ? 'Connected.' : ($timed_out ? 'Timed out.' : socket_strerror(socket_last_error($socket)));
echo 'STATUS: '. $status . PHP_EOL;