Q: Can not bind to address because it is already in use I have the following method:
    public static function listen(int $port, callable $callback) {
        ob_implicit_flush();

        if (($sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) === false) {
            println("socket_create() failed: reason: " . socket_strerror(socket_last_error()));
        }

        if (socket_bind($sock, '127.0.0.1', $port) === false) {
            println("socket_bind() failed: reason: " . socket_strerror(socket_last_error($sock)));
        }

        if (socket_listen($sock, 5) === false) {
            println("socket_listen() failed: reason: " . socket_strerror(socket_last_error($sock)));
        }

        if ($callback !== null && is_callable($callback)) {
            call_user_func($callback);
        }

        do {
            if (($msgsock = socket_accept($sock)) === false) {
                println("socket_accept() failed: reason: " . socket_strerror(socket_last_error($sock)));
                continue;
            }

            if (false === ($buf = socket_read($msgsock, 2048, PHP_BINARY_READ))) {
                println("socket_read() failed: reason: " . socket_strerror(socket_last_error($msgsock)));
                continue;
            }

            $eventName = trim(explode(' ', $buf)[1], '/');

            $msg = '';

            foreach (self::$events as $event) {
                if ($event->name() == $eventName) {
                    $msg = $event->headers();
                    $msg .= $event->run();
                    break;
                }
            }

            socket_write($msgsock, $msg, strlen($msg));
            socket_close($msgsock);
            println("socket closed");
        } while (true);

        socket_close($sock);

    }

When I run it through the commandline, it works fine. When I open a page in my browser and request content from the socket it outputs the right headers and displays the right content on the page.
When I edit the code, and press Ctrl+C and re-run the code, I get the following error:
PHP Warning:  socket_bind(): unable to bind address [98]: Address already in use in /path/to/file.php on line 20
socket_bind() failed: reason: Address already in use

This makes it hard to update code, as I have to wait a few moments until I can run the code again. Is there a way for me to close all the sockets when pressing Ctrl+C?
I noticed that I can start and stop the process lots times as long as I don't load it in the browser.

A: I don't know PHP, but in general you need to register a handler for the signal SIGINT, and close any open resources in that handler.
Please note that if you register a handler for SIGINT, Ctrl-C will no longer terminate the process (it will instead trigger the handler), so the signal handler must make sure that the program terminates, if that is what you want.
According to this answer to How do I catch a KILL or HUP or User Abort signal?, you should in PHP use pcntl_signal to register a signal had
Edit: Apparently, you must enable ticks for the signal handler to fire. A small example:
declare(ticks=1);

function sig_handler($signo)
{ 
     switch ($signo) {
         case SIGINT:
             echo "Caught SIGINT, exiting.\n";
             exit;
             break;
         default:
             // handle all other signals
             echo "Caught signal $signo...\n";
     }   

} 

// setup signal handler
pcntl_signal(SIGINT, 'sig_handler');

for ($x = 0; $x <= 50; $x++) {
    echo "main loop: $x\n";
    sleep(1);
}

The value for ticks is a trade-off between responsiveness and overhead.
