/// <summary>
        /// Builds response message and prepares byte array
        /// </summary>
        /// <param name="count">
        /// integer value that is a unique value to append to messages
        /// </param>
        /// <returns>
        /// formatted byte array
        /// </returns>
        public byte[] Response(SocketState sockState, byte[] byteMessage)
        {
            byte[] msgByte = null;

            //string msg = System.Text.Encoding.ASCII.GetString(sockState.incomingBuffer);
            string msg = System.Text.Encoding.ASCII.GetString(byteMessage);
            string[] msgArray = msg.Split('|');

            msgArray[0] = "RSP";
            msgArray[1] = sockState.stpWatch.ElapsedMilliseconds.ToString();
            msgArray[6] = sockState.sock.RemoteEndPoint.AddressFamily.ToString();
            msgArray[7] = "2605";
            msgArray[8] = sockState.sock.Handle.ToString();
            msgArray[9] = sockState.clientState.serverIPAddress1.ToString();
            msgArray[11] = "OWMid " + sockState.responseBuildCount.ToString();
            sockState.responseBuildCount++;

            string message = String.Empty;

            message = string.Join("|", msgArray);
            message.ToString();

            msgByte = System.Text.Encoding.ASCII.GetBytes(message);

            return SetMsgLength(msgByte);
        }
        /// <summary>
        /// Processes the sending funcitons of the server.
        /// This function will process messages then send
        /// them to alternate endpoint server.
        /// </summary>
        /// <param name="sockState">
        /// SocketState state saver object
        /// </param>
        private void SendFunction(SocketState sockState, byte[] messageBuffer)
        {
            try
            {
                byte[] processedMessage;
                // Have processed client message
                // need to forward this to the endpoint
                // on another socket
                ResponseBuilder rb = new ResponseBuilder();
                //sockState.clientState.processedMessage = rb.Response(sockState);
                processedMessage = rb.Response(sockState, messageBuffer);

                lock (proccessedLbAdd)
                {
                    // Add the processed message to the logbuilder
                    //sockState.clientState.lb.processedMessages.Add(sockState.clientState.processedMessage);
                    sockState.clientState.lb.processedMessages.Add(processedMessage);
                }

                // pass message to endpoint
                ClientRun cr = new ClientRun();
                Thread clientSendThread = new Thread(delegate()
                    {
                        cr.ClientSend(sockState.clientState, processedMessage);
                    });
                clientSendThread.Start();

                //-========================================
                // =---------------------------------------
                //-========================================
                // =---------------------------------------
                //-========================================
                // =---------------------------------------
                //-========================================
                // =---------------------------------------
                //Thread reRoute = new Thread(delegate()
                //{
                //    cr.RouteMessage(sockState.clientState, processedMessage);
                //});
                //reRoute.Start();
                //-========================================
                // =---------------------------------------
                //-========================================
                // =---------------------------------------
                //-========================================
                //lock (sockState.clientState.clientSendLock)
                //{
                //    // Send message back to the client
                //    //sockState.sock.Send(sockState.clientState.processedMessage);
                //    sockState.sock.Send(processedMessage);
                //}
                //-========================================
                // =---------------------------------------
                //-========================================
                // =---------------------------------------
                //-========================================
                // =---------------------------------------
                //-========================================
                // =---------------------------------------
            }
            catch (Exception e)
            {
                e.ToString();
                System.Windows.Forms.MessageBox.Show("ServerRun.SendFunction\r\n" + e.Message.ToString());
            }
        }
        /// <summary>
        /// Handles the connection that is attached to the
        /// SocketState object
        /// </summary>
        /// <param name="sockState">
        /// SocketState object to saving state values
        /// </param>
        private void ConnectionHandler(SocketState sockState)
        {
            // Incoming buffer byte array
            byte[] buffer = new byte[BUFFER_SIZE];

            // Message length byte array, stripped out of buffer
            byte[] byteSize = new byte[LENGTH_BITS];

            // bytes received last read
            int bytesRead = 0;

            int messageCount = 0;

            try
            {
                while (true)
                //while (sockState.incomingNumber < sockState.clientState.messageCount)
                //do
                {
                    // Current message receive
                    int offSet = 0;
                    int size = 0;
                    byte[] messageBuffer;

                    lock (sockState.receiveLock)
                    {
                        //while (bytesRead < 2)
                        //{
                            bytesRead = sockState.sock.Receive(buffer, offSet, LENGTH_BITS, SocketFlags.None);
                        //}
                    }
                    if (bytesRead == 0)
                    {
                        return;
                    }

                    // Get the size values out of current message
                    Array.Copy(buffer, offSet, byteSize, 0, LENGTH_BITS);

                    // Reverse the bits if they aren't in proper order for proc
                    if (BitConverter.IsLittleEndian)
                    {
                        Array.Reverse(byteSize);
                    }

                    // Set the size variable
                    size = BitConverter.ToInt16(byteSize, 0);

                    // Set offSet variable
                    offSet += LENGTH_BITS;

                    lock (sockState.receiveLock)
                    {
                        //while (bytesRead < size)
                        //{
                            // Read next message out of buffer
                            bytesRead = sockState.sock.Receive(buffer, offSet, size, SocketFlags.None);
                        //}
                    }

                    // Set messageBuffer to new byte[] with size index
                    //sockState.incomingBuffer = new byte[size];
                    messageBuffer = new byte[size];

                    // Increment the message count
                    messageCount++;
                    sockState.incomingNumber = messageCount;

                    // Copy message to messageBuffer
                    //Array.Copy(buffer, offSet, sockState.incomingBuffer, 0, size);
                    Array.Copy(buffer, offSet, messageBuffer, 0, size);

                    lock (incomingLbAdd)
                    {
                        // Add the incoming message to the logbuilder list
                        //sockState.clientState.lb.incomingMessages.Add(sockState.incomingBuffer);
                        sockState.clientState.lb.incomingMessages.Add(messageBuffer);
                    }

                    // Send message off, exit do while
                    Thread senderThread = new Thread(delegate()
                    {
                        //SendFunction(messageBuffer, sockState);
                        SendFunction(sockState, messageBuffer);
                    });

                    senderThread.Start();
                }
            }
            catch (Exception e)
            {
                //e.Message.ToString();
                System.Windows.Forms.MessageBox.Show("ServerRun.ConnectionHandler\r\n" + e.Message.ToString());
            }

            //sockState.sock.Close();
        }
        /// <summary>
        /// Accepts connections and spawns thread to handle them
        /// </summary>
        private void AcceptConnections()
        {
            Socket socket = null;

            while (true)
            {
                // Accepts a connection on socket
                try
                {
                    socket = sock.Accept();
                }
                catch (Exception e)
                {
                    e.Message.ToString();
                }

                // create and setup socket state saver
                SocketState sockState = new SocketState();
                sockState.clientState.serverIPAddress1 = IPAddress.Parse(endPoint1);
                sockState.clientState.messageCount = msgCount;
                sockState.clientState.pace = pace;
                sockState.sock = socket;
                sockState.clientState.clientSock = socket;

                // set up the client socket
                ClientRun cr = new ClientRun();
                sockState.clientState.serverSock = cr.SetClientSocket(sockState.clientState);
                //Socket endPointSocket = cr.SetClientSocket(sockState.clientState);

                // start the timer
                sockState.stpWatch.Start();

                // Spawn thread and listen on endpoint connection
                sockState.clientState.serverThread = new Thread(delegate()
                    {
                        cr.ClientReceive(sockState.clientState);
                    });
                sockState.clientState.serverThread.Start();

                // Spawns thread and starts ConnectionHandler function
                sockState.thread = new Thread(delegate()
                {
                    ConnectionHandler(sockState);
                });
                sockState.thread.IsBackground = true;
                sockState.thread.Start();
            }
        }
        /// <summary>
        /// Processes the sending funcitons of the server.
        /// This function will process messages then send
        /// them to alternate endpoint server.
        /// </summary>
        /// <param name="sockState">
        /// SocketState state saver object
        /// </param>
        private void SendFunction(SocketState sockState, byte[] messageBuffer)
        {
            try
            {
                byte[] processedMessage;
                // Have processed client message
                // need to forward this to the endpoint
                // on another socket
                ResponseBuilder rb = new ResponseBuilder();
                //sockState.clientState.processedMessage = rb.Response(sockState);
                processedMessage = rb.Response(sockState, messageBuffer);

                lock (proccessedLbAdd)
                {
                    // Add the processed message to the logbuilder
                    //sockState.clientState.lb.processedMessages.Add(sockState.clientState.processedMessage);
                    sockState.clientState.lb.processedMessages.Add(processedMessage);
                }



                // pass message to endpoint
                ClientRun cr = new ClientRun();
                Thread    clientSendThread = new Thread(delegate()
                {
                    cr.ClientSend(sockState.clientState, processedMessage);
                });
                clientSendThread.Start();



                //-========================================
                // =---------------------------------------
                //-========================================
                // =---------------------------------------
                //-========================================
                // =---------------------------------------
                //-========================================
                // =---------------------------------------
                //Thread reRoute = new Thread(delegate()
                //{
                //    cr.RouteMessage(sockState.clientState, processedMessage);
                //});
                //reRoute.Start();
                //-========================================
                // =---------------------------------------
                //-========================================
                // =---------------------------------------
                //-========================================
                //lock (sockState.clientState.clientSendLock)
                //{
                //    // Send message back to the client
                //    //sockState.sock.Send(sockState.clientState.processedMessage);
                //    sockState.sock.Send(processedMessage);
                //}
                //-========================================
                // =---------------------------------------
                //-========================================
                // =---------------------------------------
                //-========================================
                // =---------------------------------------
                //-========================================
                // =---------------------------------------
            }
            catch (Exception e)
            {
                e.ToString();
                System.Windows.Forms.MessageBox.Show("ServerRun.SendFunction\r\n" + e.Message.ToString());
            }
        }
        /// <summary>
        /// Handles the connection that is attached to the
        /// SocketState object
        /// </summary>
        /// <param name="sockState">
        /// SocketState object to saving state values
        /// </param>
        private void ConnectionHandler(SocketState sockState)
        {
            // Incoming buffer byte array
            byte[] buffer = new byte[BUFFER_SIZE];

            // Message length byte array, stripped out of buffer
            byte[] byteSize = new byte[LENGTH_BITS];

            // bytes received last read
            int bytesRead = 0;

            int messageCount = 0;

            try
            {
                while (true)
                //while (sockState.incomingNumber < sockState.clientState.messageCount)
                //do
                {
                    // Current message receive
                    int    offSet = 0;
                    int    size   = 0;
                    byte[] messageBuffer;

                    lock (sockState.receiveLock)
                    {
                        //while (bytesRead < 2)
                        //{
                        bytesRead = sockState.sock.Receive(buffer, offSet, LENGTH_BITS, SocketFlags.None);
                        //}
                    }
                    if (bytesRead == 0)
                    {
                        return;
                    }

                    // Get the size values out of current message
                    Array.Copy(buffer, offSet, byteSize, 0, LENGTH_BITS);

                    // Reverse the bits if they aren't in proper order for proc
                    if (BitConverter.IsLittleEndian)
                    {
                        Array.Reverse(byteSize);
                    }

                    // Set the size variable
                    size = BitConverter.ToInt16(byteSize, 0);

                    // Set offSet variable
                    offSet += LENGTH_BITS;

                    lock (sockState.receiveLock)
                    {
                        //while (bytesRead < size)
                        //{
                        // Read next message out of buffer
                        bytesRead = sockState.sock.Receive(buffer, offSet, size, SocketFlags.None);
                        //}
                    }

                    // Set messageBuffer to new byte[] with size index
                    //sockState.incomingBuffer = new byte[size];
                    messageBuffer = new byte[size];

                    // Increment the message count
                    messageCount++;
                    sockState.incomingNumber = messageCount;

                    // Copy message to messageBuffer
                    //Array.Copy(buffer, offSet, sockState.incomingBuffer, 0, size);
                    Array.Copy(buffer, offSet, messageBuffer, 0, size);

                    lock (incomingLbAdd)
                    {
                        // Add the incoming message to the logbuilder list
                        //sockState.clientState.lb.incomingMessages.Add(sockState.incomingBuffer);
                        sockState.clientState.lb.incomingMessages.Add(messageBuffer);
                    }

                    // Send message off, exit do while
                    Thread senderThread = new Thread(delegate()
                    {
                        //SendFunction(messageBuffer, sockState);
                        SendFunction(sockState, messageBuffer);
                    });

                    senderThread.Start();
                }
            }
            catch (Exception e)
            {
                //e.Message.ToString();
                System.Windows.Forms.MessageBox.Show("ServerRun.ConnectionHandler\r\n" + e.Message.ToString());
            }

            //sockState.sock.Close();
        }