private void HandleRead()
 {
     try
     {
         using (MemoryStream ms = new MemoryStream())
         {
             // 1 byte buffer
             // TODO: consider making this variable depending on encoding (currently will only work for UTF8)
             // ALSO: consider situation of CRLF from unix systems
             byte[] tempBuffer = new byte[1];
             tempBuffer[0] = 0x00;
             while (tempBuffer[0] != END_OF_LINE)
             {
                 int recvBytes = Handle.Receive(tempBuffer, 0, 1, SocketFlags.None);
                 if (recvBytes == 0)
                 {
                     CloseEventNotification?.Invoke(this);
                     this.Dispose();
                     return;
                 }
                 ms.Write(tempBuffer, 0, 1);
             }
             readData = ms.ToArray();
             ReadComplete();
         }
     }
     catch (SocketException se)
     {
         if (se.ErrorCode == 10054)
         {
             // socket connection forcibly closed by remote peer
             log.Error("Socket Exception Error 10054, socket connection was forcibly closed by the remote peer.");
             ErrorNotification?.Invoke(this, se);
             this.Dispose();
         }
         else
         {
             log.Error(se, "Socket Exception Error");
             ErrorNotification?.Invoke(this, se);
             this.Dispose();
         }
     }
 }
        private void HandleRead()
        {
            try
            {
                if (!recvHeader)
                {
                    byte[] headerData       = new byte[HEADER_SIZE];
                    int    headerSize       = headerData.Length;
                    int    headerLeftToRecv = headerSize;
                    int    headerSizeRecv   = 0;

                    while (headerSizeRecv != headerSize)
                    {
                        int recv = Handle.Receive(headerData, headerSizeRecv, headerLeftToRecv, SocketFlags.None);

                        // 0 value for recv means socket has closed from other end
                        if (recv != 0)
                        {
                            headerSizeRecv   += recv;
                            headerLeftToRecv -= recv;
                            lastReceivedTime  = 0;
                        }
                        else
                        {
                            CloseEventNotification?.Invoke(this);
                            this.Dispose();
                            return;
                        }
                    }

                    int headerDataValue = BitConverter.ToInt32(headerData, 0);

                    if (headerDataValue == 0)
                    {
                        // if received a keep alive packet, ignore and exit
                        log.Info("SockServiceHandler keep alive packet received.");
                        return;
                    }
                    else
                    {
                        readData   = new byte[headerDataValue];
                        recvSize   = headerDataValue;
                        leftToRecv = recvSize;
                        dataRecv   = 0;
                        recvHeader = true;
                    }
                }
                else
                {
                    int recv = Handle.Receive(readData, dataRecv, leftToRecv, SocketFlags.None);
                    if (recv == 0)
                    {
                        CloseEventNotification?.Invoke(this);
                        this.Dispose();
                        return;
                    }
                    dataRecv   += recv;
                    leftToRecv -= recv;

                    if (dataRecv == recvSize)
                    {
                        ReadComplete();
                    }
                }
            }
            catch (SocketException se)
            {
                if (se.ErrorCode == 10054)
                {
                    // socket connection forcibly closed by remote peer
                    log.Error("Socket Exception Error 10054, socket connection was forcibly closed by the remote peer.");
                    ErrorNotification?.Invoke(this, se);
                    this.Dispose();
                }
                else
                {
                    log.Error(se, "Socket Exception Error");
                    ErrorNotification?.Invoke(this, se);
                    this.Dispose();
                }
            }
        }