protected function disconnect()
    {
        // Initiate connection close. The WebSockets RFC recomends against
        // clients initiating the close handshake but we want to ensure the
        // connection is closed as soon as possible.
        $this->connection->startClose(
            Net_Notifier_WebSocket_Connection::CLOSE_GOING_AWAY,
            'Client sent message.'
        );

        // read server close frame
        $state = $this->connection->getState();

        $sec  = intval($this->timeout / 1000);
        $usec = ($this->timeout % 1000) * 1000;

        while ($state < Net_Notifier_WebSocket_Connection::STATE_CLOSED) {
            $read = array($this->socket->getRawSocket());

            $result = stream_select(
                $read,
                $write = null,
                $except = null,
                $sec,
                $usec
            );

            if ($result === 1) {
                $this->connection->read(self::READ_BUFFER_LENGTH);
            } else {
                // read timed out, just close the connection
                $this->connection->close();
            }

            $state = $this->connection->getState();
        }

        $this->connection = null;
    }