Accept a connection.

@param resource $socket
@return void
2683
public function acceptConnection($socket)
    {
        // Accept a connection on server socket.
        set_error_handler(function(){});
        $new_socket = stream_socket_accept($socket, 0, $remote_address);
        restore_error_handler();

        // Thundering herd.
        if (!$new_socket) {
            return;
        }

        // TcpConnection.
        $connection                         = new TcpConnection($new_socket, $remote_address);
        $this->connections[$connection->id] = $connection;
        $connection->worker                 = $this;
        $connection->protocol               = $this->protocol;
        $connection->transport              = $this->transport;
        $connection->onMessage              = $this->onMessage;
        $connection->onClose                = $this->onClose;
        $connection->onError                = $this->onError;
        $connection->onBufferDrain          = $this->onBufferDrain;
        $connection->onBufferFull           = $this->onBufferFull;

        // Try to emit onConnect callback.
        if ($this->onConnect) {
            try {
                call_user_func($this->onConnect, $connection);
            } catch (\Exception $e) {
                static::log($e);
                exit(250);
            } catch (\Error $e) {
                static::log($e);
                exit(250);
            }
        }
    }
1126949
public function write($data)
    {
        $this->_checkStream();
        if (!@fwrite($this->_stream, $data)) {
            $meta_data = $this->getStatus();
            if (!empty($meta_data['timed_out'])) {
                throw new Client\Exception('Timed out writing data to socket');
            }
            throw new Client\Exception('Error writing data to socket');
        }
    }
1238772
private function connecting() : void
    {
        $this->connected = false;
        $this->closing
            ? $this->closed()->resolve()
            : $this->client = Socket::connect($this->endpoint, $this->events)
        ;
    }
700528
public function handleConnect($server, $socket)
    {
        $socket->conn = new NetworkConnection($socket);

        try
        {
            $this->component->handleConnect($socket->conn);

            $socket->on('data',  [ $this, 'handleData' ]);
            $socket->on('error', [ $this, 'handleError' ]);
            $socket->on('close', [ $this, 'handleDisconnect' ]);
        }
        catch (Error $ex)
        {
            $this->close($socket);
        }
        catch (Exception $ex)
        {
            $this->close($socket);
        }
    }
261710
def accept
      while true
	s = @socket.accept
	break if (@acl ? @acl.allow_socket?(s) : true) 
	s.close
      end
      if @config[:tcp_original_host].to_s.size == 0
        uri = "druby://#{s.addr[3]}:#{@config[:tcp_port]}"
      else
        uri = @uri
      end
      self.class.new(uri, s, @config)
    end
11503
protected function makeSocket(): void
    {
        if ($this->useSSL) {
            if (!file_exists($this->cert)) {
                throw new \Exception('Cert file not found');
            }

            $context = stream_context_create([
                'ssl' => [
                    'local_cert'        => $this->cert,
                    'passphrase'        => $this->passPhrase,
                    'verify_peer'       => false,
                    'verify_peer_name'  => false,
                    'allow_self_signed' => true,
                    'verify_depth'      => 0
                ]
            ]);

            $protocol = 'ssl';
        } else {
            $context = stream_context_create();
            $protocol = 'tcp';
        }

        $this->socket = stream_socket_server("$protocol://{$this->host}:{$this->port}", $errorNumber, $errorString, STREAM_SERVER_BIND | STREAM_SERVER_LISTEN, $context);

        if ($this->useConnector) {
            if (file_exists(static::$connector)) {
                unlink(static::$connector);
            }

            $this->unixConnector = stream_socket_server('unix://' . static::$connector, $errorNumber, $errorString, STREAM_SERVER_BIND | STREAM_SERVER_LISTEN);
            chmod(static::$connector, 0777);
        }

        if (!$this->socket) {
            throw new SocketException($errorString, $errorNumber);
        }
    }
1257809
private function accept(SocketServer $server, int $cryptoMethod, float $timeout, bool $allowPersistent): \Generator
    {
        yield from $this->log->log(
            Log::INFO,
            'HTTP server listening on %s:%d',
            $server->getAddress(),
            $server->getPort()
        );

        while ($server->isOpen()) {
            try {
                $coroutine = new Coroutine(
                    $this->process(yield from $server->accept(), $cryptoMethod, $timeout, $allowPersistent)
                );
                $coroutine->done(null, $this->onError);
            } catch (Throwable $exception) {
                if ($this->isOpen()) {
                    throw $exception;
                }
            }
        }
    }
298622
private function response($socket, array $data) {
        $msg = json_encode($data, JSON_UNESCAPED_SLASHES) . "\r\n\r\n";
        @socket_write($socket, $msg, strlen($msg));
    }
377178
protected function write($buffer)
    {
        $socket = $this->getResource();

        while (($length = strlen($buffer)) > 0) {
            $written = @fwrite($socket, $buffer);

            if ($length === $written) {
                return;
            }

            if ($written === false || $written === 0) {
                $this->onConnectionError('Error while writing bytes to the server.');
            }

            $buffer = substr($buffer, $written);
        }
    }
831890
public function accept()
    {
        $res = @socket_accept($this->socket);
        if ($res === false) {
            $this->throwSocketError();
        }

        return new Socket($res);
    }
155750
public void handleConnection(Socket socket) throws IOException
    {
        // Check acceptable remote servers
        if (_remoteServers!=null&&_remoteServers.length>0)
        {
            boolean match=false;
            InetAddress inetAddress=socket.getInetAddress();
            String hostAddr=inetAddress.getHostAddress();
            String hostName=inetAddress.getHostName();
            for (int i=0; i<_remoteServers.length; i++)
            {
                if (hostName.equals(_remoteServers[i])||hostAddr.equals(_remoteServers[i]))
                {
                    match=true;
                    break;
                }
            }
            if (!match)
            {
                log.warn("AJP13 Connection from un-approved host: "+inetAddress);
                return;
            }
        }

        // Handle the connection
        socket.setTcpNoDelay(true);
        socket.setSoTimeout(getMaxIdleTimeMs());
        AJP13Connection connection=createConnection(socket);
        try
        {
            connection.handle();
        }
        finally
        {
            connection.destroy();
        }
    }
229077
public function listen() 
    {        
        $context = stream_context_create(['socket' => $this->options]);        
        if($this->getOption('SO_REUSEPORT')) {
            stream_context_set_option($context, 'socket', 'so_reuseport', 1);
        }
        
        $flag = $this->isUDP() ? STREAM_SERVER_BIND : STREAM_SERVER_BIND | STREAM_SERVER_LISTEN;
        
        if($this->is_ssl ){
            foreach($this->ssl as $name => $value){
                if($value === null) continue;
                stream_context_set_option($context, 'ssl', $name, $value);
            }
        }
        
        $errno = $errmsg = null;
        
        $this->socket = @stream_socket_server($this->socket_name, $errno, $errmsg, $flag, $context);
        if(!$this->socket || $errno) {
            $this->socket = null;
            Yii::error("$this->listen $errmsg");
            return false;
        }
        
        /**
        When you create the binding for stream_socket_server(), make sure that you choose the tcp:// wrapper. 
        DO NOT USE ssl:// or tls://. Anything other than tcp:// will not work correctly AS A SERVER, 
        those transports are what you use when making connections with PHP as a client.
         
        Remember that the encryption does not start until after an SSL handshake completed, 
        so the server has to listen in non-encrypted mode for new connections, 
        and encryption doesn't start until certs are exchanged and a cipher is selected. 
        When a new connection arrives you accept it with stream_socket_accept() and then use stream_socket_enable_crypto() to start the SSL session.        
         */
        
        if($this->is_ssl){
            stream_socket_enable_crypto($this->socket, false);            
        }
        
        if($this->isUnix()){
            list(,$sockfile) = explode('://', $this->socket_name, 2);
            $sockfile = realpath($sockfile);
            $this->socket_name = $this->transport.'://'.$sockfile;
            chmod($sockfile, 0777);
        }
        
        if (function_exists('socket_import_stream') && $this->transport === 'tcp' ) {
            $socket = socket_import_stream($this->socket);
            foreach($this->options as $name => $value) {
                if($value === null || !defined($name)) continue;
                socket_set_option($socket, SOL_SOCKET, constant($name), $value);
            }
           
            socket_set_option($socket, SOL_TCP, TCP_NODELAY, 1);
        }
        
        stream_set_blocking($this->socket, 0);
        
        stream_socket_enable_crypto($this->socket, false);
        
        Yii::debug("$this->listen listened", 'beyod');
        
        return $this;
    }
1002878
public static function assumeSocket($address, $port = 0): void
    {
        $socket = @socket_create(AF_INET, SOCK_STREAM, SOL_TCP);

        assumeThat($socket, is(not(false)), self::$SOCK_ERR_MSG . socket_last_error($socket));
        assumeThat(
            @socket_connect($socket, $address, $port),
            is(not(false)),
            'Unable to connect: ' . $address . ':' . $port
        );
    }
907139
protected function loop($socket)
    {
        while (true) {
            try {
                $result = $this->read($socket);  // is blocking
            } catch (\RuntimeException $e) {
                break;
            }
            $this->write($socket, call_user_func($this->procedure, $result));
        }
    }
964125
protected function initializeConnection($remote, $connection)
    {
        try {
            $context = $this->getContext($connection);
            $socket = $this->getSocket($remote, $context);
            $stream = $this->getStream($socket);
            $this->initializeStream($stream, $connection);
            $this->addActiveConnection($connection);
        } catch (\Exception $e) {
            $this->emitConnectionError($e, $connection);
        }

        $this->emit('connect.after.each', array($connection, $connection->getData('write')));
    }
124094
def accept(self):
        """"""
        newsock, addr = socket.accept(self)
        ssl_sock = SSLSocket(newsock._sock,
                             keyfile=self.keyfile,
                             certfile=self.certfile,
                             server_side=True,
                             cert_reqs=self.cert_reqs,
                             ssl_version=self.ssl_version,
                             ca_certs=self.ca_certs,
                             do_handshake_on_connect=self.do_handshake_on_connect,
                             suppress_ragged_eofs=self.suppress_ragged_eofs,
                             ciphers=self.ciphers)
        return ssl_sock, addr
1323437
public function setConnectionResource($resource): void
    {
        if ($this->subtypeConverter instanceof ConnectionAware) {
            $this->subtypeConverter->setConnectionResource($resource);
        }
    }