What are best practices for handling timeouts in PHP socket connections?

When working with socket connections in PHP, it's important to handle timeouts properly to prevent the script from hanging indefinitely. One common approach is to set a timeout value for the socket connection using the `socket_set_option` function. This allows you to control how long the script will wait for a response before timing out.

// Set a timeout value for the socket connection
socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, array("sec" => 5, "usec" => 0));