private void DataReceivedCallback(System.IAsyncResult ar)
        {
            // Retrieve the state object and the client socket
            // from the asynchronous state object.
            SocketStateObject StateObject = (SocketStateObject)ar.AsyncState;


            //* If the socket was closed, then we cannot do anything
            if (!StateObject.WorkSocket.Connected)
            {
                return;
            }

            //* Get the number of bytes read and add it to the state object accumulator
            try
            {
                //* Add the byte count to the state object
                StateObject.CurrentIndex += StateObject.WorkSocket.EndReceive(ar);
            }
            catch (Exception ex)
            {
                //* Return an error code
                OnComError("Socket Error : " + ex.Message);
                return;
            }

            lock (DataReceivedLock)
            {
                // Console.WriteLine("DataReceived Received - Index=" & StateObject.CurrentIndex)

                //*************************************************************************************************************************
                int  i           = 0;
                byte CurrentByte = 0;

                while (i < StateObject.CurrentIndex)
                {
                    CurrentByte = StateObject.data[i];

                    if (CurrentByte == m_TerminatingByteValue)
                    {
                        byte[] dataArray  = ReceivedDataPacketBuilder.ToArray();
                        string dataString = string.Empty;
                        for (var index2 = 0; index2 < dataArray.Length; index2++)
                        {
                            if (dataArray[index2] >= 32 && dataArray[index2] < 128)
                            {
                                dataString += Microsoft.VisualBasic.Strings.Chr(dataArray[index2]).ToString();
                            }
                        }
                        OnDataReceived(new GenericTcpEventArgs(dataArray, dataString));

                        CurrentByte = 0; //make sure last byte isn't falsely 16
                        ReceivedDataPacketBuilder.Clear();
                    }
                    else
                    {
                        ReceivedDataPacketBuilder.Add(CurrentByte);
                    }

                    i += 1;
                }
            }

            SocketStateObject so = new SocketStateObject(WorkSocket);

            WorkSocket.BeginReceive(so.data, 0, so.data.Length, SocketFlags.None, DataReceivedCallback, so);
        }
        //*********************************************
        //* Connect to the socket and begin listening
        //* for responses
        //********************************************
        private void Connect()
        {
            System.Net.IPEndPoint  endPoint = null;
            System.Net.IPHostEntry IP       = null;

            System.Net.IPAddress address = new System.Net.IPAddress(0);
            if (System.Net.IPAddress.TryParse(m_IPAddress, out address))
            {
                endPoint = new System.Net.IPEndPoint(address, m_Port);
            }
            else
            {
                try
                {
                    IP = System.Net.Dns.GetHostEntry(m_IPAddress);
                    //* Ethernet/IP uses port AF12 (44818)
                    endPoint = new System.Net.IPEndPoint(IP.AddressList[0], m_Port);
                }
                catch (Exception ex)
                {
                    throw new FormatException("Can't resolve the address " + m_IPAddress);
                    return;
                }
            }


            if (WorkSocket == null || !WorkSocket.Connected)
            {
                if (m_ProtocolType == System.Net.Sockets.ProtocolType.Tcp)
                {
                    WorkSocket = new System.Net.Sockets.Socket(endPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
                    //* Comment these out for Compact Framework
                    WorkSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, 5000);
                    WorkSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);
                }
                else
                {
                    WorkSocket = new System.Net.Sockets.Socket(endPoint.AddressFamily, SocketType.Dgram, m_ProtocolType);
                }

                WorkSocket.SendTimeout       = 2000;
                WorkSocket.ReceiveBufferSize = 0x5000;
            }

            try
            {
                try
                {
                    WorkSocket.Connect(endPoint);
                }
                catch (SocketException ex)
                {
                    //* Return an error code
                    OnComError("Could Not Connect to Server : " + ex.Message);

                    CloseConnection();
                    throw;
                }


                OnConnectionEstablished(System.EventArgs.Empty);

                StartTestThread();
            }
            catch (SocketException ex)
            {
                // 10035 == WSAEWOULDBLOCK
                if (ex.NativeErrorCode.Equals(10035))
                {
                    //Throw
                }
                else
                {
                    throw; //New Exception(m_IPAddress & " " & ex.Message)
                }
            }


            WorkSocket.Blocking = true;
            if (m_ProtocolType == System.Net.Sockets.ProtocolType.Tcp)
            {
                WorkSocket.LingerState = new System.Net.Sockets.LingerOption(true, 1000);
            }


            //* Don't buffer the data, so it goes out immediately
            //* Otherwise packets send really fast will get grouped
            //* And the PLC will not respond to all of them
            WorkSocket.SendBufferSize = 1;

            SocketStateObject so = new SocketStateObject();

            so.WorkSocket = WorkSocket;

            WorkSocket.BeginReceive(so.data, 0, so.data.Length, SocketFlags.None, DataReceivedCallback, so);
        }