public function parse($data)
    {
        if ($this->state === self::STATE_UNSENT) {
            $this->state = self::STATE_OPENED;
        }

        if ($this->isHeaderComplete) {
            $data = $this->parseData($data);
        } else {
            $data = $this->parseHeader($data);
        }

        if ($this->cursor == $this->getLength() + $this->headerLength) {
            $this->state = self::STATE_DONE;
        }

        return $data;
    }