private function selectConnections(&$readSockets, int $timeout): void
    {
        // stream_select will not always preserve array keys
        // call it with a (deep) copy so the original is preserved
        $read = [];
        foreach ($readSockets as $id => $socket) {
            $read[] = $socket;
        }
        $writeSockets = $exceptSockets = [];

        if (false === @stream_select($read, $writeSockets, $exceptSockets, $timeout)) {
            $error = error_get_last();

            if (false === stripos($error['message'], 'interrupted system call')) {
                throw new \RuntimeException('stream_select failed: '.$error['message']);
            }

            $readSockets = [];
        } else {
            $res = [];
            foreach($read as $socket) {
                $res[array_search($socket, $readSockets)] = $socket;
            }
            $readSockets = $res;
        }
    }