/**
  * @param QuarkClient $client
  * @param string $data
  *
  * @return mixed
  * @throws \Exception
  */
 public function OnData(QuarkClient $client, $data)
 {
     if ($client->Connected()) {
         try {
             $this->_buffer .= $data;
             $out = $this->_processor->Decode($this->_buffer);
             if ($out !== false) {
                 $this->_buffer = '';
                 if ($this->_protocol) {
                     $this->_protocol->OnData($client, $out);
                 }
             }
         } catch (\Exception $e) {
             $this->_buffer = '';
         }
     } else {
         if ($data != "\r\n") {
             $this->_buffer .= $data;
             return;
         }
         $request = new QuarkDTO();
         $request->UnserializeRequest($this->_buffer . "\r\n");
         $this->_buffer = '';
         $response = new QuarkDTO(new QuarkHTMLIOProcessor());
         $response->Status(101, 'Switching Protocols');
         $response->Headers(array(QuarkDTO::HEADER_CONNECTION => QuarkDTO::CONNECTION_UPGRADE, QuarkDTO::HEADER_UPGRADE => QuarkDTO::UPGRADE_WEBSOCKET, QuarkDTO::HEADER_SEC_WEBSOCKET_ACCEPT => base64_encode(sha1($request->Header(QuarkDTO::HEADER_SEC_WEBSOCKET_KEY) . self::GuID, true))));
         if (strlen($this->_subprotocol) != 0) {
             $response->Header(QuarkDTO::HEADER_SEC_WEBSOCKET_PROTOCOL, $this->_subprotocol);
         }
         $client->Send($response->SerializeResponse());
         $client->Connected(true);
         $client->BeforeSend(function ($data) {
             return $this->_processor->Encode($data);
         });
         if ($this->_protocol) {
             $this->_protocol->OnConnect($client);
         }
     }
 }