/// <summary>
        /// The event receiver thread. This thread never exits.
        /// </summary>
        private void ReceiveEventThread()
        {
            string server  = "";
            int    port    = 0;
            string login   = "";
            bool   unicode = false;

            while (true)
            {
                try
                {
                    Thread.Sleep(10000);

                    lock (AppSettings.Default)
                    {
                        server  = AppSettings.Default.setFLHookIP;
                        port    = Convert.ToInt32(AppSettings.Default.setFLHookPort);
                        login   = AppSettings.Default.setFLHookPassword;
                        unicode = AppSettings.Default.setFLHookUnicode;
                        debug   = AppSettings.Default.setDebug;
                    }

                    // Do nothing if flhook is disabled.
                    if (port == 0)
                    {
                        break;
                    }

                    // Establish the socket connection
                    stReplyBuf = "";

                    CloseSocket();
                    lock (locker)
                    {
                        rxSocket = new TcpClient(server, port);
                        rxSocket.ReceiveTimeout = 5000;
                        rxStream = rxSocket.GetStream();
                    }

                    if (debug > 0)
                    {
                        log.AddLog("flhook: opened connection");
                    }

                    // Wait for the welcome message.
                    if (ReceiveReply(rxStream, unicode) != "Welcome to FLHack, please authenticate")
                    {
                        throw new Exception("no login message");
                    }

                    // Send the pass and wait for OK
                    SendCommand(rxStream, String.Format("pass {0}", login), unicode);
                    if (ReceiveReply(rxStream, unicode) != "OK")
                    {
                        throw new Exception("no pass ok message");
                    }

                    // ASk hook to enter event mode.
                    SendCommand(rxStream, "eventmode", unicode);
                    if (ReceiveReply(rxStream, unicode) != "OK")
                    {
                        throw new Exception("no eventmode ok message");
                    }

                    if (debug > 0)
                    {
                        log.AddLog("flhook: login complete");
                    }

                    // Loop receiving events
                    byte[] rxBuf = new byte[1000];
                    rxSocket.ReceiveTimeout = 0;

                    // Otherwise read some bytes from the stream.
                    string reply = ReceiveReply(rxStream, unicode);
                    while (reply != null)
                    {
                        string[] keys;
                        string[] values;
                        FLHookSocket.ParseLine(reply, out keys, out values);
                        eventListener.ReceiveFLHookEvent(keys[0], keys, values, reply);

                        reply = ReceiveReply(rxSocket.GetStream(), unicode);
                    }
                }
                catch (ThreadAbortException)
                {
                    if (debug > 0)
                    {
                        log.AddLog("flhook: shutdown connection");
                    }
                }
                catch (ThreadInterruptedException)
                {
                    if (debug > 0)
                    {
                        log.AddLog("flhook: configuration reset");
                    }
                }
                catch (Exception ex)
                {
                    if (debug > 0)
                    {
                        log.AddLog("flhook: '" + ex.Message + "'");
                    }
                }
                finally
                {
                    if (debug > 0)
                    {
                        log.AddLog("flhook: closing connection");
                    }
                    stReplyBuf = "";
                    try
                    {
                        CloseSocket();
                    }
                    catch { }
                }
            }
        }