protected function send($data, $opcode = WebsocketPayload::OPCODE_TEXT, $masked = true)
    {
        $rawMessage = json_encode($data);
        $payload = new WebsocketPayload();
        $payload
            ->setOpcode($opcode)
            ->setMask($masked)
            ->setPayload($rawMessage);

        $encoded = $payload->encodePayload();

        if (0 === @fwrite($this->socket, $encoded)) { //connection reseted by peers, just reconnect.
            $this->connected = false;
            $this->connect($this->target);

            fwrite($this->socket, $encoded);
        }

        return $this;
    }