public function receivePacket($bufferLength = 0) {
        if(!$this->socket->select(self::$timeout)) {
            throw new TimeoutException();
        }

        if($bufferLength == 0) {
            $this->buffer->clear();
        } else {
            $this->buffer = ByteBuffer::allocate($bufferLength);
        }

        try {
            $data = $this->socket->recv($this->buffer->remaining());
            $this->buffer->put($data);
        } catch (ConnectionResetException $e) {
            $this->socket->close();
            throw $e;
        }

        $bytesRead = $this->buffer->position();
        $this->buffer->rewind();
        $this->buffer->limit($bytesRead);

        return $bytesRead;
    }