Hi. I'm trying to write a simple and minimalist Http proxy server that can on the command line. In Start() method, a simple TcpListener blocks until it gets a client request and creates a new thread (ThreadHandleClient method) that processes this client, fetches its url and relays data.

The trouble is with the relay logic. What happens is that in the proxy-client (browser), I type a url (www.google.com) which opens fine. Then I perform a keyword search which also goes fine. However, when I click on a search-result, I'm again seeing the google.com page, (though my browser url text still shows the result page). I think this is due to my relay logic. Somehow, the client socket is still receiving the old request instead of new one. Can you help me find out what is wrong it?

Thanks in advance. This is my code:

Code:
public void Start(IPAddress ip, int port)
        {
            TcpListener listener = new TcpListener(ip, port);
            listener.Start(100);
            while (!stopFlag)
            {
                Socket client = listener.AcceptSocket();
                IPEndPoint rep = (IPEndPoint)client.RemoteEndPoint;
                clients.Add(rep.Address.ToString());
                Thread th = new Thread(ThreadHandleClient);
                th.Start(client);
            }

            listener.Stop();
        }

        public void ThreadHandleClient(object o)
        {
            try
            {
                Socket client = (Socket)o;
                NetworkStream ns = new NetworkStream(client);
                //RECEIVE CLIENT DATA
                byte[] buffer = new byte[2048];
                int rec = 0, sent = 0, transferred = 0, rport = 0;
                string data = "";
                do
                {
                    rec = ns.Read(buffer, 0, buffer.Length);
                    data += Encoding.ASCII.GetString(buffer, 0, rec);
                } while (rec == buffer.Length);

                //PARSE DESTINATION AND SEND REQUEST
                string line = data.Replace("\r\n", "\n").Split(new string[] { "\n" }, StringSplitOptions.None)[0];
                Uri uri = new Uri(line.Split(new string[] { " " }, StringSplitOptions.None)[1]);
                if (uri.Scheme == "https")
                {
                    rport = 443;

                    //rq = HttpVersion + " 200 Connection established\r\nProxy-Agent: Prahlad`s Proxy Server\r\n\r\n";
                    //ClientSocket.BeginSend(Encoding.ASCII.GetBytes(rq), 0, rq.Length, SocketFlags.None, new AsyncCallback(this.OnOkSent), ClientSocket);
                }
                else
                {
                    rport = 80;
                }
                IPHostEntry rh = Dns.GetHostEntry(uri.Host);
                Socket webserver = new Socket(rh.AddressList[0].AddressFamily, SocketType.Stream, ProtocolType.IP);
                webserver.Connect(new IPEndPoint(rh.AddressList[0], rport));
                byte[] databytes = Encoding.ASCII.GetBytes(data);
                webserver.Send(databytes, databytes.Length, SocketFlags.None);

                //START RELAY
                buffer = new byte[2048];
                rec = 0;
                data = "";
                do
                {
                    transferred = 0;
                    do
                    {
                        rec = webserver.Receive(buffer, buffer.Length, SocketFlags.None);
                        sent = client.Send(buffer, rec, SocketFlags.None);
                        transferred += rec;
                        //data += Encoding.ASCII.GetString(serverbytes, 0, rec);
                    } while (rec == buffer.Length);

                    if (transferred == 0)
                        break;

                    transferred = 0;
                    do
                    {
                        rec = client.Receive(buffer, buffer.Length, SocketFlags.None);
                        sent = webserver.Send(buffer, sent, SocketFlags.None);
                        transferred += rec;
                    } while (rec == buffer.Length);

                } while (transferred > 0);
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.Print("Error occured: " + ex.Message);
            }
        }