How do I prevent Socket/Port Exhaustion?
===

I am attempting to performance test a website by hitting it with requests across multiple threads. Each thread executes *n* times. (in a for loop)

However, I am running into problems. Specifically the WebException ("Unable to connect to remote server") with the inner exception:

> An operation on a socket could not be performed because the system
> lacked sufficient buffer space or because a queue was full
> 127.0.0.1:52395

I am attempting to run 100 threads at 500 iterations per thread. 

Initially I was using `HttpWebRequest` in System.Net to make the GET request to the server. Currently I am using `WebClient` as I assumed that each iteration was using a new socket (so 100 * 500 sockets in a short period of time). I assumed WebClient (which is instantiated once per thread) would only use one socket.

However, even with WebClient, a bunch of sockets are being requested resulting in a bunch of sockets in `TIME_WAIT` mode (checked using netstat). This causes other applications (like internet browsers) to hang and stop functioning.

I can operate my test with less iterations and/or less threads, as it appears the sockets do eventually exit this TIME_WAIT state. However, this is not a solution as it doesn't adequately test the abilities of the web server.

**Question:**

How do I explicitly close a socket (from the client side) in order to prevent TIME_WAIT states and socket exhaustion?


**Code:**

Class that wraps the HttpRequest

    public sealed class HttpGetTest : ITest {
    	private readonly string m_url;
    	private readonly WebClient m_webClient;
    
    	public HttpGetTest( string url ) {
    		m_webClient = new WebClient();
    		m_url = url;		
    	}
    
    	public void ITest.Execute() {
             //This is where the exception occurs
    		using( Stream stream = m_webClient.OpenRead( m_url ) ) {
    			
    		}
    	}
    }

The part of my ThreadWrapperClass that creates a new thread:

    public void Execute() {
    	Action Hammer = () => {
    		for( int i = 1; i <= m_iterations; i++ ) {
    			//Where m_test is an ITest injected through constructor
    			m_test.Execute();
    		}		
    	};
    	ThreadStart work = delegate {
    		Hammer();
    	};
    	Thread thread = new Thread( work );
    	thread.Start();
    }