private void ProcessControlCommandThread()
        {
            while (processThreadRuning)
            {
                try
                {
                    if (!listenServer.Pending())
                    {
                        Thread.Sleep(500);
                        continue;
                    }
                    TcpClient     client = listenServer.AcceptTcpClient();
                    NetworkStream stream = client.GetStream();
                    SUT_Control_Request_Message requestMessage = new SUT_Control_Request_Message();
                    List <byte> receivedDataList = new List <byte>();
                    DateTime    end    = DateTime.Now + timeout;
                    byte[]      buffer = new byte[bufferLength];
                    while (DateTime.Now < end)
                    {
                        int readbyteNumber = 0;
                        do
                        {
                            readbyteNumber = stream.Read(buffer, 0, bufferLength);
                            if (readbyteNumber > 0)
                            {
                                byte[] tmpbuffer = new byte[readbyteNumber];
                                Array.Copy(buffer, tmpbuffer, readbyteNumber);
                                receivedDataList.AddRange(tmpbuffer);
                            }
                        }while (readbyteNumber == bufferLength);
                        int index = 0;
                        if (requestMessage.Decode(receivedDataList.ToArray(), ref index))
                        {
                            Console.WriteLine("Receive command: " + (RDPSUTControl_CommandId)requestMessage.commandId);
                            SUT_Control_Response_Message response = RDPClientControl.ProcessCommand(requestMessage);
                            byte[] sendData = response.Encode();
                            stream.Write(sendData, 0, sendData.Length);
                            break;
                        }
                    }

                    stream.Close();
                    client.Close();
                    // Run post operation if it is existed. used for auto-reconnect
                    if (RDPClientControl.PostOperation != null)
                    {
                        RDPClientControl.PostOperation();
                        RDPClientControl.PostOperation = null;
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine("Exception found during listening:" + e.Message);
                }
            }
        }
        /// <summary>
        /// Process SUT Control Command
        /// </summary>
        /// <param name="requestMessage"></param>
        /// <returns></returns>
        public static SUT_Control_Response_Message ProcessCommand(SUT_Control_Request_Message requestMessage)
        {
            PostOperation = null;
            if (requestMessage == null)
            {
                throw new ArgumentNullException("SUT_Control_Request_Message inputed is null");
            }
            if (requestMessage.messageType != SUTControl_MessageType.SUT_CONTROL_REQUEST || requestMessage.testsuiteId != SUTControl_TestsuiteId.RDP_TESTSUITE)
            {
                throw new ArgumentException("Not available request message." + requestMessage.messageType + "," + requestMessage.testsuiteId);
            }

            RDPSUTControl_CommandId commandId = (RDPSUTControl_CommandId)requestMessage.commandId;

            byte[] payload      = null;
            string errorMessage = null;
            uint   resultCode   = 1;

            try
            {
                switch (commandId)
                {
                case RDPSUTControl_CommandId.START_RDP_CONNECTION:
                    RDP_Connection_Payload rdpPayload = new RDP_Connection_Payload();
                    int index = 0;
                    if (rdpPayload.Decode(requestMessage.payload, (int)requestMessage.payloadLength, ref index))
                    {
                        if (rdpPayload.type == RDP_Connect_Payload_Type.RDP_FILE)
                        {
                            if (Start_RDP_Connection(rdpPayload.rdpFileConfig) > 0)
                            {
                                resultCode = (uint)SUTControl_ResultCode.SUCCESS;
                            }
                        }
                        else
                        {
                            if (Start_RDP_Connection(rdpPayload.configureParameters) > 0)
                            {
                                resultCode = (uint)SUTControl_ResultCode.SUCCESS;
                            }
                        }
                    }
                    break;

                case RDPSUTControl_CommandId.CLOSE_RDP_CONNECTION:
                    if (Close_RDP_Connection() > 0)
                    {
                        resultCode = (uint)SUTControl_ResultCode.SUCCESS;
                    }
                    break;

                case RDPSUTControl_CommandId.AUTO_RECONNECT:
                    resultCode    = (uint)SUTControl_ResultCode.SUCCESS;
                    PostOperation = AUTO_RECONNECT;
                    break;

                case RDPSUTControl_CommandId.SCREEN_SHOT:
                    if (TAKE_SCREEN_SHOT(out payload) > 0)
                    {
                        resultCode = (uint)SUTControl_ResultCode.SUCCESS;
                    }
                    break;

                default:
                    errorMessage = "SUT control agent doesn't support this command:" + commandId;
                    break;
                }
            }
            catch (Exception e)
            {
                errorMessage = "Exception found when process " + commandId + "," + e.Message;
            }

            SUT_Control_Response_Message responseMessage = new SUT_Control_Response_Message(SUTControl_TestsuiteId.RDP_TESTSUITE, (ushort)commandId, requestMessage.caseName, requestMessage.requestId, resultCode, errorMessage, payload);

            return(responseMessage);
        }