/// <summary>
        /// A method is used to restart the HTTP listener. It will dispose the original HTTP listener and then re-generate a HTTP listen instance to listen request.
        /// </summary>
        protected void RestartListener()
        {
            lock (threadLockObjectForAppendLog)
            {
                DiscoveryProcessHelper.AppendLogs(currentHelperType, DateTime.Now, "Try to restart the Httplistener.");
            }

            // If it is not in "stop" status, try to restart the Httplistener.
            if (ListenInstance.IsListening)
            {
                ListenInstance.Stop();
            }

            while (ListenInstance.IsListening)
            {
                // sleep zero, so that other thread can get the CPU timespan in a valid
                Thread.Sleep(0);
            }

            // Release the original HttpListener resource.
            ListenInstance.Abort();
            ((IDisposable)ListenInstance).Dispose();
            ListenInstance = null;

            // Restart a new HttpListener instance.
            ListenInstance = new HttpListener();
            this.SetPrefixForListener(ListenInstance);
        }
        /// <summary>
        /// A method is used to set the prefix for listener.
        /// </summary>
        /// <param name="listenerInstance">A parameter represents the HttpListener instance which will be set the listened prefix.</param>
        protected void SetPrefixForListener(HttpListener listenerInstance)
        {
            if (null == listenerInstance)
            {
                throw new ArgumentNullException("listenerInstance");
            }

            if (listenerInstance.IsListening)
            {
                throw new InvalidOperationException("The listener has been started, could not set listened prefix for it when it is in started status.");
            }

            // The discovery request must send to "http://{0}/hosting/discovery" URL, and the listener's prefix should append the "/" after the url.
            string listenedprefix = string.Format(@"http://{0}/hosting/discovery/", this.HostNameOfDiscoveryService);
            Uri    uri            = new Uri(listenedprefix);

            listenedprefix = uri.GetComponents(UriComponents.SchemeAndServer, UriFormat.SafeUnescaped);
            ListenInstance.Prefixes.Add(listenedprefix + "/");
            ListenInstance.Start();

            lock (threadLockObjectForAppendLog)
            {
                DiscoveryProcessHelper.AppendLogs(currentHelperType, DateTime.Now, string.Format("Start the HttpListener [{0}] succeed.", Thread.CurrentThread.ManagedThreadId));
            }
        }
        /// <summary>
        /// A method is used to stop listen process. This method will abort the thread which is listening discovery request and release all resource are used by the thread.
        /// </summary>
        public void StopListen()
        {
            lock (threadLockStaticObjectForVisitThread)
            {
                // If the listen thread has not been start, skip the stop operation.
                if (!hasStartListenThread)
                {
                    return;
                }

                this.IsRequiredStop = true;

                lock (threadLockObjectForAppendLog)
                {
                    DiscoveryProcessHelper.AppendLogs(currentHelperType, DateTime.Now, string.Format("Stop the listening thread.The listening thread managed Id[{0}]", listenThreadHandle.ManagedThreadId));
                }

                if (listenThreadHandle != null && listenThreadHandle.ThreadState != ThreadState.Unstarted &&
                    ListenInstance != null)
                {
                    lock (threadLockObjectForAppendLog)
                    {
                        // Close the http listener and release the resource used by listener. This might cause the thread generate exception and then the thread will be expected to end and join to the main thread.
                        ListenInstance.Abort();
                        ((IDisposable)ListenInstance).Dispose();
                        DiscoveryProcessHelper.AppendLogs(currentHelperType, DateTime.Now, string.Format("Release the Httplistener resource. The listening thread managed Id[{0}]", listenThreadHandle.ManagedThreadId));
                    }

                    // Wait the thread join to the main caller thread.
                    TimeSpan listenThreadJoinTimeOut = new TimeSpan(0, 0, 1);
                    bool     isthreadEnd             = listenThreadHandle.Join(listenThreadJoinTimeOut);

                    // If the thread could not end as expected, abort this thread.
                    if (!isthreadEnd)
                    {
                        if ((listenThreadHandle.ThreadState & (ThreadState.Stopped | ThreadState.Unstarted)) == 0)
                        {
                            listenThreadHandle.Abort();
                            lock (threadLockObjectForAppendLog)
                            {
                                DiscoveryProcessHelper.AppendLogs(currentHelperType, DateTime.Now, string.Format("Abort the listening thread. The listening thread managed Id[{0}]", listenThreadHandle.ManagedThreadId));
                            }
                        }
                    }

                    // Set the static status to tell other instance, the listen thread has been abort.
                    hasStartListenThread = false;
                    listenThreadHandle   = null;
                }
            }
        }
        /// <summary>
        /// A method is used to restart the HTTP listener. It will dispose the original HTTP listener and then re-generate a HTTP listen instance to listen request.
        /// </summary>
        protected void RestartListener()
        {
            lock (threadLockObjectForAppendLog)
            {
                DiscoveryProcessHelper.AppendLogs(currentHelperType, DateTime.Now, "Try to restart the Httplistener.");
            }
            // Release the original HttpListener resource.
            ListenInstance.Stop();
            ListenInstance = null;

            // Restart a new TcpListener instance.
            IPAddress  iPAddress = IPAddress.Any;
            IPEndPoint endPoint  = new IPEndPoint(iPAddress, 80);

            ListenInstance = new TcpListener(endPoint);
        }
        /// <summary>
        /// A method is used to listening the discovery request. It will be executed by a thread which is started on ListenThreadInstance method.
        /// </summary>
        protected void ListenToRequest()
        {
            ListenInstance.Start();

            // If the listener is listening, just keep on execute below code.
            while (hasStartListenThread)
            {
                try
                {
                    TcpClient client = ListenInstance.AcceptTcpClient();
                    if (client.Connected == true)
                    {
                        Console.WriteLine("Created connection");
                    }
                    // if the calling thread requires stopping the listening mission, just return and exit the loop. This value of "IsrequireStop" property is managed by "StopListen" method.
                    if (this.IsRequiredStop)
                    {
                        break;
                    }

                    lock (threadLockStaticObjectForVisitThread)
                    {
                        // Double check the "IsrequireStop" status.
                        if (this.IsRequiredStop)
                        {
                            break;
                        }
                    }

                    lock (threadLockObjectForAppendLog)
                    {
                        string logMsg = string.Format("Listening............ The listen thread: managed id[{0}].", Thread.CurrentThread.ManagedThreadId);
                        DiscoveryProcessHelper.AppendLogs(currentHelperType, DateTime.Now, logMsg);
                    }
                    NetworkStream netstream = client.GetStream();
                    try
                    {
                        byte[] buffer = new byte[2048];

                        int    receivelength = netstream.Read(buffer, 0, 2048);
                        string requeststring = Encoding.UTF8.GetString(buffer, 0, receivelength);

                        if (!requeststring.StartsWith(@"GET /hosting/discovery", StringComparison.OrdinalIgnoreCase))
                        {
                            break;
                        }
                    }
                    catch (Exception ex)
                    {
                        lock (threadLockObjectForAppendLog)
                        {
                            DiscoveryProcessHelper.AppendLogs(currentHelperType, DateTime.Now, string.Format("The listen thread catches an [{0}] exception:[{1}].", ex.GetType().Name, ex.Message));
                        }

                        lock (threadLockStaticObjectForVisitThread)
                        {
                            if (this.IsRequiredStop)
                            {
                                lock (threadLockObjectForAppendLog)
                                {
                                    DiscoveryProcessHelper.AppendLogs(currentHelperType, DateTime.Now, "Requires stopping the Httplistener.");
                                }

                                return;
                            }
                            else
                            {
                                this.RestartListener();
                            }
                        }
                    }
                    bool writeResponseSucceed = false;
                    try
                    {
                        string statusLine = "HTTP/1.1 200 OK\r\n";
                        byte[] responseStatusLineBytes = Encoding.UTF8.GetBytes(statusLine);
                        string responseHeader          =
                            string.Format(
                                "Content-Type: text/xml; charset=UTf-8\r\nContent-Length: {0}\r\n", this.ResponseDiscovery.Length);
                        byte[] responseHeaderBytes = Encoding.UTF8.GetBytes(responseHeader);
                        byte[] responseBodyBytes   = Encoding.UTF8.GetBytes(this.ResponseDiscovery);
                        writeResponseSucceed = true;
                        netstream.Write(responseStatusLineBytes, 0, responseStatusLineBytes.Length);
                        netstream.Write(responseHeaderBytes, 0, responseHeaderBytes.Length);
                        netstream.Write(new byte[] { 13, 10 }, 0, 2);
                        netstream.Write(responseBodyBytes, 0, responseBodyBytes.Length);
                        client.Close();
                    }
                    catch (Exception ex)
                    {
                        lock (threadLockObjectForAppendLog)
                        {
                            DiscoveryProcessHelper.AppendLogs(currentHelperType, DateTime.Now, string.Format("The listen thread catches an [{0}] exception:[{1}] on responding.", ex.GetType().Name, ex.Message));
                        }

                        lock (threadLockStaticObjectForVisitThread)
                        {
                            if (this.IsRequiredStop)
                            {
                                lock (threadLockObjectForAppendLog)
                                {
                                    DiscoveryProcessHelper.AppendLogs(currentHelperType, DateTime.Now, string.Format("Catch an exception:[{0}]. Current requires stopping the Httplistener. Thread managed Id[{1}].", ex.Message, Thread.CurrentThread.ManagedThreadId));
                                }

                                return;
                            }
                            else
                            {
                                this.RestartListener();
                            }
                        }
                    }

                    if (writeResponseSucceed)
                    {
                        lock (threadLockStaticObjectForVisitThread)
                        {
                            // Setting the status.
                            if (!hasResponseDiscoveryRequestSucceed)
                            {
                                hasResponseDiscoveryRequestSucceed = true;
                            }
                        }

                        lock (threadLockObjectForAppendLog)
                        {
                            DiscoveryProcessHelper.AppendLogs(
                                currentHelperType,
                                DateTime.Now,
                                string.Format(
                                    "Response the discovery requestsucceed! The listen thread managedId[{0}]",
                                    Thread.CurrentThread.ManagedThreadId));
                        }
                    }
                }
                catch (SocketException ee)
                {
                    DiscoveryProcessHelper.AppendLogs(
                        currentHelperType,
                        DateTime.Now,
                        string.Format("SocketException: {0}", ee.Message));
                }
            }
        }
        /// <summary>
        /// A method is used to listening the discovery request. It will be executed by a thread which is started on ListenThreadInstance method.
        /// </summary>
        protected void ListenToRequest()
        {
            // If the listener is listening, just keep on execute below code.
            while (ListenInstance.IsListening)
            {
                // if the calling thread requires stopping the listening mission, just return and exit the loop. This value of "IsrequireStop" property is managed by "StopListen" method.
                if (this.IsRequiredStop)
                {
                    break;
                }

                lock (threadLockStaticObjectForVisitThread)
                {
                    // Double check the "IsrequireStop" status.
                    if (this.IsRequiredStop)
                    {
                        break;
                    }
                }

                lock (threadLockObjectForAppendLog)
                {
                    string logMsg = string.Format("Listening............ The listen thread: managed id[{0}].", Thread.CurrentThread.ManagedThreadId);
                    DiscoveryProcessHelper.AppendLogs(currentHelperType, DateTime.Now, logMsg);
                }

                // Get an incoming request.
                HttpListenerContext  listenContext            = null;
                HttpListenerResponse responseOfCurrentRequest = null;
                HttpListenerRequest  currentRequest           = null;

                try
                {
                    listenContext            = ListenInstance.GetContext();
                    currentRequest           = listenContext.Request as HttpListenerRequest;
                    responseOfCurrentRequest = listenContext.Response as HttpListenerResponse;

                    if (!currentRequest.RawUrl.Equals(@"/hosting/discovery", StringComparison.OrdinalIgnoreCase))
                    {
                        break;
                    }
                }
                catch (Exception ex)
                {
                    lock (threadLockObjectForAppendLog)
                    {
                        DiscoveryProcessHelper.AppendLogs(currentHelperType, DateTime.Now, string.Format("The listen thread catches an [{0}] exception:[{1}].", ex.GetType().Name, ex.Message));
                    }

                    lock (threadLockStaticObjectForVisitThread)
                    {
                        if (this.IsRequiredStop)
                        {
                            lock (threadLockObjectForAppendLog)
                            {
                                DiscoveryProcessHelper.AppendLogs(currentHelperType, DateTime.Now, "Requires stopping the Httplistener.");
                            }

                            return;
                        }
                        else
                        {
                            this.RestartListener();
                        }
                    }
                }

                if (responseOfCurrentRequest != null)
                {
                    responseOfCurrentRequest.ContentType     = "text/xml";
                    responseOfCurrentRequest.ContentEncoding = Encoding.UTF8;
                    responseOfCurrentRequest.StatusCode      = 200;

                    // Get the xml response.
                    XmlDocument reponseXml = new XmlDocument();
                    reponseXml.LoadXml(this.ResponseDiscovery);
                    bool writeResponseSucceed = false;
                    try
                    {
                        using (XmlTextWriter xmlWriter = new XmlTextWriter(responseOfCurrentRequest.OutputStream, Encoding.UTF8))
                        {
                            reponseXml.Save(xmlWriter);
                            writeResponseSucceed = true;
                        }
                    }
                    catch (Exception ex)
                    {
                        lock (threadLockObjectForAppendLog)
                        {
                            DiscoveryProcessHelper.AppendLogs(currentHelperType, DateTime.Now, string.Format("The listen thread catches an [{0}] exception:[{1}] on responding.", ex.GetType().Name, ex.Message));
                        }

                        lock (threadLockStaticObjectForVisitThread)
                        {
                            if (this.IsRequiredStop)
                            {
                                lock (threadLockObjectForAppendLog)
                                {
                                    DiscoveryProcessHelper.AppendLogs(currentHelperType, DateTime.Now, string.Format("Catch an exception:[{0}]. Current requires stopping the Httplistener. Thread managed Id[{1}].", ex.Message, Thread.CurrentThread.ManagedThreadId));
                                }

                                return;
                            }
                            else
                            {
                                this.RestartListener();
                            }
                        }
                    }

                    if (writeResponseSucceed)
                    {
                        lock (threadLockStaticObjectForVisitThread)
                        {
                            // Setting the status.
                            if (!hasResponseDiscoveryRequestSucceed)
                            {
                                hasResponseDiscoveryRequestSucceed = true;
                            }
                        }

                        lock (threadLockObjectForAppendLog)
                        {
                            DiscoveryProcessHelper.AppendLogs(
                                currentHelperType,
                                DateTime.Now,
                                string.Format(
                                    "Response the discovery request from [{0}] succeed! The listen thread managedId[{1}]",
                                    currentRequest.UserHostName,
                                    Thread.CurrentThread.ManagedThreadId));
                        }
                    }
                }
            }
        }