public function processQueue()
    {
        if (count($this->commandQueue) === 0 && $this->queryState === static::STATE_READY && $this->autoDisconnect) {
            $this->commandQueue[] = new Terminate();
        }

        if (count($this->commandQueue) === 0) {
            return;
        }

        if ($this->connStatus === $this::CONNECTION_BAD) {
            $this->failAllCommandsWith(new \Exception('Bad connection: ' . $this->lastError));
            if ($this->stream) {
                $this->stream->end();
                $this->stream = null;
            }
            return;
        }

        while (count($this->commandQueue) > 0 && $this->queryState === static::STATE_READY) {
            /** @var CommandInterface $c */
            $c = array_shift($this->commandQueue);
            if (!$c->isActive()) {
                continue;
            }
            $this->debug('Sending ' . get_class($c));
            if ($c instanceof Query) {
                $this->debug('Sending simple query: ' . $c->getQueryString());
            }
            $this->stream->write($c->encodedMessage());
            if ($c instanceof Terminate) {
                $this->stream->end();
            }
            if ($c->shouldWaitForComplete()) {
                $this->setState($this::STATE_BUSY);
                if ($c instanceof Query) {
                    $this->queryType = $this::QUERY_SIMPLE;
                } elseif ($c instanceof Sync) {
                    $this->queryType = $this::QUERY_EXTENDED;
                }

                $this->currentCommand = $c;

                return;
            }
        }
    }