/// <summary>
 /// Stop this service.
 /// </summary>
 protected override void OnStop()
 {
     lock (_lockObject)
     {
         if (_socket != null)
         {
             _socket.Stop();
             _socket = null;
         }
     }
     ServiceDebugger.WriteLogEntry(Name, "Stopping.");
 }
        //private void StopThread()
        //{
        //    m_WorkerThread.Abort();
        //}

        private void WorkerThreadFunction()
        {
            try
            {
                lock (_lockObject)
                {
                    bool returnIdle = true;
                    ////bool.TryParse(ConfigurationManager.AppSettings["ReturnIdle"], out returnIdle);

                    Mode   mode    = Mode.Normal;
                    string modeStr = ConfigurationManager.AppSettings["Mode"];
                    mode = string.Equals(Mode.Drain.ToString(), modeStr, StringComparison.OrdinalIgnoreCase) ? Mode.Drain :
                           (string.Equals(Mode.Halt.ToString(), modeStr, StringComparison.OrdinalIgnoreCase) || string.Equals("down", modeStr, StringComparison.OrdinalIgnoreCase) ? Mode.Halt : Mode.Normal);

                    _socket = new SocketLib()
                    {
                        Alive = new Action <SocketLib>(socketLib =>
                        {
                            if (ReadRegKeyInt(RegKeyName, "AliveLogging", 0) > 0)
                            {
                                //Write to event log IF should
                                ServiceDebugger.WriteLogEntry(Name, String.Format("Listening on port {0}", socketLib.Port));
                            }
                        }),
                        Error = new Action <Exception>(exception =>
                        {
                            if (ReadRegKeyInt(RegKeyName, "ExceptionLogging", 0) > 0)
                            {
                                ServiceDebugger.WriteLogEntry(Name, exception.ToString());
                            }
                        }),
                        ReturnIdle = returnIdle,
                        Mode       = mode
                    };
                }
                _socket.Start();
            }
            catch (Exception ex)
            {
                ServiceDebugger.WriteLogEntry(Name, ex.ToString());
            }
            finally
            {
                //Tell OS we've stopped
                Stop();
            }
        }