Called when socket is bound
@return boolean|null Success
510500
protected function onBound()
    {
        if ($this->ev) {
            $this->ev->getSocketName($this->locHost, $this->locPort);
        } else {
            Daemon::log('Unable to bind TCP-socket ' . $this->host . ':' . $this->getPort());
        }
    }
1026155
protected function connect()
    {
        try {
            // Loop until we connect
            while (!$this->isConnected()) {
                // Assume we will connect, until we dont
                $this->setIsConnected(true);

                // Connect the socket
                $this->io->connect();

                $this->channels = array();
                // The connection object itself is treated as channel 0
                parent::__construct($this, 0);

                $this->input = new AMQPReader(null, $this->io);

                $this->write($this->amqp_protocol_header);
                $this->wait(array($this->waitHelper->get_wait('connection.start')),false,$this->connection_timeout);
                $this->x_start_ok(
                    $this->getLibraryProperties(),
                    $this->login_method,
                    $this->login_response,
                    $this->locale
                );

                $this->wait_tune_ok = true;
                while ($this->wait_tune_ok) {
                    $this->wait(array(
                        $this->waitHelper->get_wait('connection.secure'),
                        $this->waitHelper->get_wait('connection.tune')
                    ));
                }

                $host = $this->x_open($this->vhost, '', $this->insist);
                if (!$host) {
                    //Reconnected
                    $this->io->reenableHeartbeat();
                    return null; // we weren't redirected
                }

                $this->setIsConnected(false);
                $this->closeChannels();

                // we were redirected, close the socket, loop and try again
                $this->close_socket();
            }

        } catch (\Exception $e) {
            // Something went wrong, set the connection status
            $this->setIsConnected(false);
            $this->closeChannels();
            $this->close_input();
            $this->close_socket();
            throw $e; // Rethrow exception
        }
    }
1101333
public function recvChr() {
        if (!$this->_sock) { throw new SocketClientException("Connection gone!"); }
        $char = fgetc($this->_sock);
        if ($this->stream_eof()) { return false; }
        if ($char === TEL_IAC && $this->negotiation_enabled) {
            $this->_negotiate(fgetc($this->_sock));
            return "";
        }
        $this->_buff .= $char;
        return $char;
     }
550265
public function disconnect() {
        if ($this->socket) {
            if (!fclose($this->socket)) {
                throw new \Exception("Error while closing telnet socket");
            }
            $this->socket = NULL;
        }
        return true;
    }
404967
public function unbind()
    {
        if ($this->isBinded()) {
            $this->getSocket()->unbind($this->dsn);
            $this->dsn = null;
        }

        return $this;
    }
1100110
function open() {
        // let's try to open a socket
        $this->_error_log('open a socket connection');
        $this->sock = fsockopen($this->_socket . $this->_server, $this->_port, $this->_errno, $this->_errstr, $this->_socket_timeout);
        core_php_time_limit::raise(30);
        if (is_resource($this->sock)) {
            socket_set_blocking($this->sock, true);
            $this->_connection_closed = false;
            $this->_error_log('socket is open: ' . $this->sock);
            return true;
        } else {
            $this->_error_log("$this->_errstr ($this->_errno)\n");
            return false;
        }
    }
1238772
private function connecting() : void
    {
        $this->connected = false;
        $this->closing
            ? $this->closed()->resolve()
            : $this->client = Socket::connect($this->endpoint, $this->events)
        ;
    }
745835
public function isConnectionClose()
    {
        $connection = $this->getHeader('connection');

        if ($this->protocolVersion === '1.1') {
            return ($connection[0] === 'C' || $connection[0] === 'c');
        } else {
            return ($connection[0] !== 'K' && $connection[0] !== 'k');
        }
    }
177786
public function onReady()
    {
        $appInstance = $this; // a reference to this application instance for ExampleWebSocketRoute
        \PHPDaemon\Servers\WebSocket\Pool::getInstance()->addRoute('ExamplePubSub',
            function ($client) use ($appInstance) {
                return new ExamplePubSubWebSocketRoute($client, $appInstance);
            });
        $this->sql = \PHPDaemon\Clients\MySQL\Pool::getInstance();
        $this->pubsub = new \PHPDaemon\PubSub\PubSub();
        $this->pubsub->addEvent('usersNum', \PHPDaemon\PubSub\PubSubEvent::init()
            ->onActivation(function ($pubsub) use ($appInstance) {
                \PHPDaemon\Core\Daemon::log('onActivation');
                if (isset($pubsub->event)) {
                    \PHPDaemon\Core\Timer::setTimeout($pubsub->event, 0);
                    return;
                }
                $pubsub->event = setTimeout(function ($timer) use ($pubsub, $appInstance) {
                    $appInstance->sql->getConnection(function ($sql) use ($pubsub) {
                        if (!$sql->connected) {
                            return;
                        }
                        $sql->query('SELECT COUNT(*) `num` FROM `dle_users`', function ($sql, $success) use ($pubsub) {
                            $pubsub->pub(sizeof($sql->resultRows) ? $sql->resultRows[0]['num'] : 'null');
                        });
                    });
                    $timer->timeout(5e6); // 5 seconds
                }, 0);
            })
            ->onDeactivation(function ($pubsub) {
                if (isset($pubsub->event)) {
                    \PHPDaemon\Core\Timer::cancelTimeout($pubsub->event);
                }
            })
        );
    }
1073395
public function checkConnection($socket)
    {
        // Remove EV_EXPECT for windows.
        if(DIRECTORY_SEPARATOR === '\\') {
            Worker::$globalEvent->del($socket, EventInterface::EV_EXCEPT);
        }
        // Check socket state.
        if ($address = stream_socket_get_name($socket, true)) {
            // Remove write listener.
            Worker::$globalEvent->del($socket, EventInterface::EV_WRITE);
            // Nonblocking.
            stream_set_blocking($socket, 0);
            // Compatible with hhvm
            if (function_exists('stream_set_read_buffer')) {
                stream_set_read_buffer($socket, 0);
            }
            // Try to open keepalive for tcp and disable Nagle algorithm.
            if (function_exists('socket_import_stream') && $this->transport === 'tcp') {
                $raw_socket = socket_import_stream($socket);
                socket_set_option($raw_socket, SOL_SOCKET, SO_KEEPALIVE, 1);
                socket_set_option($raw_socket, SOL_TCP, TCP_NODELAY, 1);
            }
            // Register a listener waiting read event.
            Worker::$globalEvent->add($socket, EventInterface::EV_READ, array($this, 'baseRead'));
            // There are some data waiting to send.
            if ($this->_sendBuffer) {
                Worker::$globalEvent->add($socket, EventInterface::EV_WRITE, array($this, 'baseWrite'));
            }
            $this->_status                = self::STATUS_ESTABLISHED;
            $this->_remoteAddress         = $address;
            $this->_sslHandshakeCompleted = true;

            // Try to emit onConnect callback.
            if ($this->onConnect) {
                try {
                    call_user_func($this->onConnect, $this);
                } catch (\Exception $e) {
                    Worker::log($e);
                    exit(250);
                } catch (\Error $e) {
                    Worker::log($e);
                    exit(250);
                }
            }
            // Try to emit protocol::onConnect
            if (method_exists($this->protocol, 'onConnect')) {
                try {
                    call_user_func(array($this->protocol, 'onConnect'), $this);
                } catch (\Exception $e) {
                    Worker::log($e);
                    exit(250);
                } catch (\Error $e) {
                    Worker::log($e);
                    exit(250);
                }
            }
        } else {
            // Connection failed.
            $this->emitError(WORKERMAN_CONNECT_FAIL, 'connect ' . $this->_remoteAddress . ' fail after ' . round(microtime(true) - $this->_connectStartTime, 4) . ' seconds');
            if ($this->_status === self::STATUS_CLOSING) {
                $this->destroy();
            }
            if ($this->_status === self::STATUS_CLOSED) {
                $this->onConnect = null;
            }
        }
    }
1247994
public static void isSocketBindable(InetSocketAddress addr)
      throws IOException {
    if (addr == null) {
      return;
    }
    ServerSocket socket = new ServerSocket();
    try {
      socket.bind(addr);
    } finally {
      socket.close();
    }
  }
251289
public function attachBound(\PHPDaemon\BoundSocket\Generic $bound, $inf = null)
    {
        $this->bound->attach($bound, $inf);
        if ($this->eventLoop !== null) {
            $bound->setEventLoop($this->eventLoop);
        }
    }
1235665
public function onKernelRequest()
    {
        if (!is_null($this->host)) {
            $this->router->getContext()->setHost($this->host);
        }

        if (!is_null($this->portHttp)) {
            $this->router->getContext()->setHttpPort($this->portHttp);
        }

        if (!is_null($this->portHttps)) {
            $this->router->getContext()->setHttpsPort($this->portHttps);
        }
    }
45549
public function closeSocket()
    {
        if (is_resource($this->resource)) {
            fclose($this->resource);
            $this->resource = null;
        }
    }
722537
protected function _closeSocket() {
        if ($this->debug_) {
            error_log("Closing socket.");
        }

        $fd = $this->socket_;
        if (is_resource($fd)) {
            $this->socket_ = null;
            @fclose($fd);
        }
    }
1215158
public function connected()
    {
        if (is_resource($this->smtp_conn)) {
            $sock_status = stream_get_meta_data($this->smtp_conn);
            if ($sock_status['eof']) {
                // The socket is valid but we are not connected
                $this->edebug(
                    'SMTP NOTICE: EOF caught while checking if connected',
                    self::DEBUG_CLIENT
                );
                $this->close();

                return false;
            }

            return true; // everything looks good
        }

        return false;
    }
504224
public function bind()
    {

        $err = $errno = 0;

        $this->FLASH_POLICY_FILE = str_replace('to-ports="*', 'to-ports="' . $this->uri->getPort() ?: 80, $this->FLASH_POLICY_FILE);

        $serverSocket = stream_socket_server($this->uri->toString(), $errno, $err, STREAM_SERVER_BIND | STREAM_SERVER_LISTEN, $this->_context);

        $this->_logger->notice(sprintf("phpws listening on %s", $this->uri->toString()));

        if ($serverSocket == false) {
            $this->_logger->err("Error: $err");
            return;
        }

        $timeOut = & $this->purgeUserTimeOut;
        $sockets = $this->_streams;
        $that = $this;
        $logger = $this->_logger;

        $this->loop->addReadStream($serverSocket, function ($serverSocket) use ($that, $logger, $sockets) {
            try
            {
                $newSocket = stream_socket_accept($serverSocket);
            } catch (\ErrorException $e) {
                $newSocket = false;
            }

            if (false === $newSocket) {
                return;
            }

            stream_set_blocking($newSocket, 0);
            $client = new WebSocketConnection($newSocket, $that->loop, $logger);
            $sockets->attach($client);

            $client->on("handshake", function(Handshake $request) use($that, $client){
                $that->emit("handshake",array($client->getTransport(), $request));
            });

            $client->on("connect", function () use ($that, $client, $logger) {
                $con = $client->getTransport();
                $that->getConnections()->attach($con);
                $that->emit("connect", array("client" => $con));
            });

            $client->on("message", function ($message) use ($that, $client, $logger) {
                $connection = $client->getTransport();
                $that->emit("message", array("client" => $connection, "message" => $message));
            });

            $client->on("close", function () use ($that, $client, $logger, &$sockets) {
                $sockets->detach($client);
                $connection = $client->getTransport();

                if($connection){
                    $that->getConnections()->detach($connection);
                    $that->emit("disconnect", array("client" => $connection));
                }
            });

            $client->on("flashXmlRequest", function () use ($that, $client) {
                $client->getTransport()->sendString($that->FLASH_POLICY_FILE);
                $client->close();
            });
        });

        $this->loop->addPeriodicTimer(5, function () use ($timeOut, $sockets, $that) {

            # Lets send some pings
            foreach($that->getConnections() as $c){
                if($c instanceof WebSocketTransportHybi)
                    $c->sendFrame(WebSocketFrame::create(WebSocketOpcode::PingFrame));
            }

            $currentTime = time();
            if ($timeOut == null)
                return;

            foreach ($sockets as $s) {
                if ($currentTime - $s->getLastChanged() > $timeOut) {
                    $s->close();
                }
            }
        });
    }