I am using sockets to send data to a server that may not be responding. So I am trying to define a timeout by using this solution in SO.
Make PHP socket_connect timeout
socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, array('sec' => 1, 'usec' => 0)); socket_set_option($socket, SOL_SOCKET, SO_SNDTIMEO, array('sec' => 1, 'usec' => 0)); This works when the connection is made and the server takes too long to respond. But when it can't create a connection socket_connect($socket, $addr, $port); the timeout is about 20 seconds.
Why is this 20 second timeout happening and how can I force the connection creation to timeout after 1 second too?