private void Exit()
        {
            // We must manually tidy up and remove the icon before we exit.
            // Otherwise it will be left behind until the user mouses over.
            _icon.Visible = false;

            // Abort all threads
            if (_serverThread != null && _serverThread.IsAlive)
            {
                _serverThread.Abort();
            }
            if (_luaThread != null && _luaThread.IsAlive)
            {
                _luaThread.Abort();
            }
            //_gtaThread.Abort();
            if (_clientThread != null && _clientThread.IsAlive)
            {
                _clientThread.Abort();
            }

            LuaScripting.CloseScripts();
            Application.Exit();
        }
 private void Application_ApplicationExit(object sender, EventArgs e)
 {
     LuaScripting.CloseScripts();
     Exit();
 }
        private void ParseData(TcpClient listener)
        {
            // Buffer for reading data
            Byte[] bytes = new Byte[256];

            try
            {
                // Enter the listening loop.
                Debug.Write("Got a connection... ");

                // Perform a blocking call to accept requests.
                // You could also user server.AcceptSocket() here.
                Debug.WriteLine("Connected!");


                // Get a stream object for reading and writing
                NetworkStream stream = listener.GetStream();


                byte[]        myReadBuffer      = new byte[1024];
                StringBuilder myCompleteMessage = new StringBuilder();
                int           numberOfBytesRead = 0;

                // Incoming message may be larger than the buffer size.
                do
                {
                    numberOfBytesRead = stream.Read(myReadBuffer, 0, myReadBuffer.Length);

                    myCompleteMessage.AppendFormat("{0}", Encoding.ASCII.GetString(myReadBuffer, 0, numberOfBytesRead));
                }while (stream.DataAvailable);
                if (myCompleteMessage.Length != 0)
                {
                    // Print out the received message to the console.
                    try
                    {
                        var split = myCompleteMessage.ToString().Split(new char[] { '{' }, 2);

                        if (split.Length > 1)
                        {
                            String ns = "{" + myCompleteMessage.ToString().Split(new char[] { '{' }, 2)[1];
                            myCompleteMessage = null;
                            //Debug.Write(myCompleteMessage);
                            string header = string.Format("HTTP/1.1 {0}\r\n"
                                                          + "Server: {1}\r\n"
                                                          + "Content-Type: {2}\r\n"
                                                          + "Keep-Alive: Close\r\n"
                                                          + "\r\n",
                                                          "HTTP 200 OK", "Chroma Sync", "application/json");
                            // Send header & data
                            var headerBytes = Encoding.ASCII.GetBytes(header);
                            stream.Write(headerBytes, 0, headerBytes.Length);

                            try
                            {
                                JObject o = JObject.Parse(ns);
                                LuaScripting.PassThrough(o);
                            }
                            catch (Exception e)
                            {
                                Debug.WriteLine(e);
                            }
                            return;
                        }
                    }
                    catch (Exception e)
                    {
                        Debug.WriteLine(e);
                    }
                }
            }
            catch { }
            finally
            {
                try
                {
                    listener.Close();
                }
                catch (Exception) { }
            }
        }
 void ReloadScripts(object sender, EventArgs e)
 {
     LuaScripting.ReloadScripts();
 }