public function close(int $code = Protocol::CLOSE_NORMAL, string $reason = null): bool
    {
        try {
            if (!$this->handshaked) {
                $response = $this->protocol->getResponseError($code);
                $this->socket->send($response);
            } else {
                $response = $this->protocol->getClosePayload($code, false);
                $response->sendToSocket($this->socket);
            }
        } catch (Throwable $e) {
            $this->logger->warning('Unable to send close message');
        }

        if ($this->application && method_exists($this->application, 'onDisconnect')) {
            $this->application->onDisconnect($this);
        }

        $this->socket->disconnect();
        $this->manager->removeConnection($this);

        return true;
    }