private void Send(ExtensionRequestPacket packet, string data, string sendAsContentType)
        {
            if (packet.PacketType == ChromeExtensionPacketType.Post)
            {
                // Console.WriteLine("Sending to {0}:\n{1}", packet.ID, data);
                // Reset the signal so that the processor will wait for another
                // POST message.
                lock (responseLock)
                {
                    postRequestReceived.Reset();
                    receivedResponse = false;
                }
            }

            byte[] byteData = Encoding.UTF8.GetBytes(data);
            HttpListenerResponse response = packet.Context.Response;
            response.KeepAlive = true;
            response.StatusCode = 200;
            response.StatusDescription = "OK";
            response.ContentType = sendAsContentType;
            response.ContentLength64 = byteData.LongLength;
            response.Close(byteData, true);
        }
        private void OnClientConnect(IAsyncResult asyncResult)
        {
            try
            {
                HttpListener listener = (HttpListener)asyncResult.AsyncState;

                // Here we complete/end the BeginGetContext() asynchronous call
                // by calling EndGetContext() - which returns the reference to
                // a new HttpListenerContext object. Then we can set up a new
                // thread to listen for the next connection.
                HttpListenerContext workerContext = listener.EndGetContext(asyncResult);
                listener.BeginGetContext(OnClientConnect, listener);

                ExtensionRequestPacket packet = new ExtensionRequestPacket(workerContext);

                // Is the request asking for a connection to extension?
                Connecting = workerContext.Request.QueryString["doConnect"] != null;

                // Console.WriteLine("ID {0} connected.", packet.ID);
                if (packet.PacketType == ChromeExtensionPacketType.Get)
                {
                    // Console.WriteLine("Received GET request from from {0}", packet.ID);
                    if (!Connected && Connecting)
                        Send(packet, HostPageHtml, "text/html");
                    else
                        Send(packet, BlankPageHtml, "text/html");
                }
                else
                {
                    lock (responseLock)
                    {
                        // Console.WriteLine("Received from {0}:\n{1}", packet.ID, packet.Content);
                        // pendingRequestQueue.Enqueue(packet);
                        pendingRequestQueue.Add(packet);
                        postRequestReceived.Set();
                        receivedResponse = true;
                    }
                }

                // Console.WriteLine("ID {0} disconnected.", packet.ID);
            }
            catch (ObjectDisposedException)
            {
                //Utilities.WriteToConsole("ChromeHttpServer was disposed.");
            }
            catch (SocketException e)
            {
                Utilities.WriteToConsole(FormatOnClientConnectException(e));
            }
            catch (HttpListenerException e)
            {
                Utilities.WriteToConsole(FormatOnClientConnectException(e));
            }
        }