public function writeHead()
{
if (self::STATE_WRITING_HEAD <= $this->state) {
throw new \LogicException('Headers already written');
}
$this->state = self::STATE_WRITING_HEAD;
$requestData = $this->requestData;
$streamRef =& $this->stream;
$stateRef =& $this->state;
$this->connect()->done(function ($stream) use($requestData, &$streamRef, &$stateRef) {
$streamRef = $stream;
$stream->on('drain', array($this, 'handleDrain'));
$stream->on('data', array($this, 'handleData'));
$stream->on('end', array($this, 'handleEnd'));
$stream->on('error', array($this, 'handleError'));
$headers = (string) $requestData;
$stream->write($headers);
$stateRef = Request::STATE_HEAD_WRITTEN;
$this->emit('headers-written', array($this));
}, array($this, 'handleError'));
}