Using the Stream Socket Extension in PHP
As of PHP 5.0.0 the stream extension (providing all of PHP's IO abstraction) is able to bind and connect to network sockets. Socket resources created with the stream extensions can be used with almost all stream related functions such as fgets, fread or stream_get_contents.
Creating a server socket using the stream_socket_accept function is very easy and can be done with just one parameter. The only requirement is that the socket address be specified in standard URL format: transport://target. The target portion should either be a hostname or IP address followed by a colon and a port number or if the socket is an Unix domain socket it should be a filesystem path (see Appendix J for a list of supported transports).
The second parameter of the stream_socket_accept() function is a timeout value, which specifies how long the stream_socket_accept() call will wait before returning if no data has been received from the connected socket. If the second parameter is omitted, the default socket accept timeout is 5 seconds.
Using a timeout value of 0 allows you to instantly poll the status of the streams, but this may cause your script to use too much CPU. For this reason it is recommended to use a reasonable timeout value and always pass a context to the stream_socket_accept(), which contains any filter or other options you want applied to the stream. A context is a set of parameters and wrapper specific options that modify or enhance the behavior of a stream and can be created with stream_context_create().