Hi there, this is - I think - my third post on this subject, which means I was still not able to implement my proxy server.
For the people that did not read my other posts, I am trying to use the .NET framework to create a proxy server that intercepts all requests to a certain port and redirects them to an indicated address (indicates by me), appending all the correct credentials (and the purpose of all this thing is to handle the authentication, so that the clients dont need to worry about it). I created a class to implement the server, and I call it like this, inside a thread.

				m_prx = new ProxyServer(this);
				System.Threading.Thread proxythread = new System.Threading.Thread(new ThreadStart(m_prx.Start));
Inside the Start() method, I need to have some sort of listener, waiting for connections; on a first approach, I used a high level implementation that relies on httplistener class:

				System.Diagnostics.Debug.WriteLine("Listening socket on port " + m_port);
				while (true)
					HttpListenerContext request = m_listener.GetContext();
					ThreadPool.QueueUserWorkItem(ProcessRequest, request);
This relies on the threadPool, which means .NET handles the multithreading at OS level;
The function Process Request is, in the essence quite simple: it issues a webrequest (actually httpWebrequest) and appends all the stuff (credentials, headers, etc); then,

						HttpWebRequest request = (HttpWebRequest)WebRequest.Create(address);
						//create cache object
						request.Credentials = AddCache(uri);
Then, it grabbs the stream:

						buffer = new byte[BUFFER_SIZE];
						Stream instream = context.Request.InputStream;
						int incount = instream.Read(buffer, 0, buffer.Length);
						while (incount > 0)
							request.GetRequestStream().Write(buffer, 0, incount);
							incount = instream.Read(buffer, 0, buffer.Length);
						// Wait for Response
						WebResponse response = request.GetResponse();
and it transfers it into the response:

						// Transfer the body data from the WebResponse
						buffer = new byte[BUFFER_SIZE];
						Stream outstream = response.GetResponseStream();
						int outcount = outstream.Read(buffer, 0, buffer.Length);
						while (outcount > 0)
							context.Response.OutputStream.Write(buffer, 0, outcount);
							outcount = outstream.Read(buffer, 0, buffer.Length);
						// Close streams
There is some missing code here, but this is pretty much the general idea.
It works pretty well, and it is quite fast. However, sometimes (and I believe it is when there are sycnhronous or almost syncrhonous requests from clients) it ends up in this situation (exception):

A first chance exception of type 'System.Net.HttpListenerException' occurred in System.dll
An operation was attempted on a nonexistent network connection
I thought it could be a problem with the server, so I tested it with a different server but no luck: same error, when multiple requests were thrown almost at the same time.
(I also thought the firewall might be intercepting something here, so I disabled it but it also did not have any influence).
So I guess the problem would be with my code...

My perspective was that the httplistener class was not really tackling the multithreading, which I need to have working properly because of the simultaneous requests from clients...

After this, I tried different implementations:
- an asynchronous GetContext (beginGetContext)
- implementation with real sockets: TCPListener and finally raw sockets;

The implementation with sockets is more or less like this:

				TcpListener listener = new TcpListener(IPAddress.Any, m_port);

(I also use a ManualResetEvent here to send signals about the threads)
Then the callback:

	public static void DoBeginAcceptSocket(TcpListener listener)
		// Set the event to nonsignaled state.

		// Start to listen for connections from a client.
		//Console.WriteLine("Waiting for a connection...");

		// Accept the connection. 
		// BeginAcceptSocket() creates the accepted socket.
			new AsyncCallback(ProcessSocket), listener);
		// Wait until a connection is made and processed before 
		// continuing.

And the callback "ProcessSocket", is a bit similar to "ProcessRequest", except that is a bit more complex; In the end, since we are not inside a while loop, I need to call DoBeginAcceptSocket again;

This approach, also did not got me out of the problem... now I get a different error, which I think is related to the same issue (except is a different class of exception):

A first chance exception of type 'System.Net.Sockets.SocketException' occurred in System.dll
An existing connection was forcibly closed by the remote host.
(I also tried a version with the sinchronous "AcceptSocket", with the same results);
I am runnning a bit out of ideas here, about what may be the problem... has anyone incurred into this before? I would really appreciate any tips/suggestions on this that could put me into the right direction...
I have to add that I am able to catch this error and carry on (even if I have to restart the listener) and in many cases is not a problem (apart from that the user does not see some output cause the requests didnt went trought...) however... not happy about this!
Thanks in advance and have a good weekend!