public function onStateEv($bev, $events)
    {
        if ($events & \EventBufferEvent::CONNECTED) {
            $this->onWriteEv($bev);
        } elseif ($events & \EventBufferEvent::TIMEOUT) {
            $this->timedout = true;
            $this->finish();
        } elseif ($events & (\EventBufferEvent::ERROR | \EventBufferEvent::EOF)) {
            try {
                if ($this->finished) {
                    return;
                }
                if ($events & \EventBufferEvent::ERROR) {
                    $errno = \EventUtil::getLastSocketErrno();
                    if ($errno !== 0 && $errno !== 104) {
                        $this->log('Socket error #' . $errno . ':' . \EventUtil::getLastSocketError());
                    }
                    if ($this->ssl && $this->bev) {
                        while ($err = $this->bev->sslError()) {
                            $this->log('EventBufferEvent SSL error: ' . $err);
                        }
                    }
                }
                $this->finished = true;
                $this->onFinish();
                $this->close();
            } catch (\Exception $e) {
                Daemon::uncaughtExceptionHandler($e);
            }
        }
    }