/// <summary>
        ///   Connect to the remote endpoint with the specified timeout on
        ///   receiving the response that will transition the connection to
        ///   the connected state.
        /// </summary>
        /// <param name="remoteEP"></param>
        /// <param name="millisecondsTimeout">number of milliseconds to wait, or -1 to wait indefinitely</param>
        /// <returns></returns>
        public RdpConnection Connect(IPEndPoint remoteEP, int millisecondsTimeout)
        {
            RdpConnection conn    = GetConnection(udpConn, remoteEP, false);
            bool          success = false;

            log_status.InfoFormat("Created connection to: {0}", remoteEP);
            try {
                success = conn.WaitForState(ConnectionState.Open, millisecondsTimeout);
                if (!success)
                {
                    throw new TimeoutException();
                }
            } catch (Exception) {
                ReleaseConnection(conn);
                throw;
            }
            return(conn);
        }
        private static void Main(string[] args)
        {
            SetupDebug();
            if (args.Length != 3 && args.Length != 4) {
                Logit("usage: TestRDPClient hostname remotePort localPort <run-incoming-thread>");
                Trace.Flush();
            }

            remoteHostname = args[0];
            remotePort = int.Parse(args[1]);
            localPort = int.Parse(args[2]);

            for (int i=0; i<1; i++) {
                rdpClient = new RdpClient(localPort + i, 100, 1000, true);
                IPHostEntry IPHost = Dns.GetHostEntry(remoteHostname);
                IPAddress[] addr = IPHost.AddressList;
                IPEndPoint sendpt = new IPEndPoint(addr[0], remotePort);
                Logit("Connecting to host " + remoteHostname + ", remotePort " + remotePort + ", localPort " + localPort);
                rdpConnection = rdpClient.Connect(sendpt);
                rdpConnection.WaitForState(ConnectionState.Open);
                Logit("Connection successful");
                Thread outgoingThread = new Thread(RunOutgoing);
                outgoingThread.Start();
                if (args.Length == 4) {
                    Thread incomingThread = new Thread(RunIncoming);
                    incomingThread.Start();
                }
                while (outgoingThread.IsAlive)
                    Thread.Sleep(500);
            }
            Trace.Flush();
        }
        private static void Main(string[] args)
        {
            SetupDebug();
            if (args.Length != 1 && args.Length != 2) {
                Logit("usage: TestRDPServer localPort <messageCount>");
                Trace.Flush();
            }
            else {

                serverPort = int.Parse(args[0]);

                rdpServer = new RdpServer(serverPort, 100, 1000, true);
                rdpConnection = rdpServer.Accept();
                Logit("Accepted client connection; server state = " + rdpConnection.ConnectionState);
                rdpConnection.WaitForState(ConnectionState.Open);
                for (int i=0; i<1; i++) {
                    Thread incomingThread = new Thread(RunIncoming);
                    incomingThread.Start();
                    if (args.Length == 2) {
                        Thread outgoingThread = new Thread(RunOutgoing);
                        outgoingThread.Start();
                    }
                    while (incomingThread.IsAlive)
                        Thread.Sleep(500);
                    Logit("incomingThread.IsAlive = " + incomingThread.IsAlive);
                }
            }
            Trace.Flush();
        }