Index: binary-improvements/7dtd-server-fixes/src/NetConnections/Servers/Web/Web.cs
===================================================================
--- binary-improvements/7dtd-server-fixes/src/NetConnections/Servers/Web/Web.cs	(revision 199)
+++ binary-improvements/7dtd-server-fixes/src/NetConnections/Servers/Web/Web.cs	(revision 202)
@@ -3,4 +3,5 @@
 using System.IO;
 using System.Net;
+using System.Net.Sockets;
 using System.Text;
 using System.Threading;
@@ -15,4 +16,6 @@
 		private bool authEnabled = false;
 		private string realm = "7dtd Admin Panel";
+		public static int handlingCount = 0;
+		public static int currentHandlers = 0;
 
 		public Web ()
@@ -62,21 +65,7 @@
 				_listener.Realm = realm;
 
-				ThreadPool.QueueUserWorkItem ((o) =>
-				{
-					try {
-						while (_listener.IsListening) {
-							ThreadPool.QueueUserWorkItem ((c) =>
-							{
-								HttpListenerContext ctx = c as HttpListenerContext;
-								HandleRequest (ctx);
-							}, _listener.GetContext ());
-						}
-					} catch {
-					}
-				}
-				);
-
 				NetTelnetServer.RegisterServer (this);
 
+				_listener.BeginGetContext (new AsyncCallback (HandleRequest), _listener);
 
 				Log.Out ("Started Webserver on " + (webPort + 2) + " (authentication " + (authEnabled ? "enabled" : "disabled") + ")");
@@ -86,40 +75,51 @@
 		}
 
-		private void HandleRequest (HttpListenerContext ctx)
+		private void HandleRequest (IAsyncResult result)
 		{
-			try {
-				ctx.Response.ProtocolVersion = new Version ("1.1");
+			if (_listener.IsListening) {
+				Interlocked.Increment(ref handlingCount);
+				Interlocked.Increment(ref currentHandlers);
+				HttpListenerContext ctx = _listener.EndGetContext (result);
+				_listener.BeginGetContext (new AsyncCallback (HandleRequest), _listener);
+				try {
+					ctx.Response.ProtocolVersion = new Version ("1.1");
 
-				HttpListenerBasicIdentity user = Authorize (ctx);
+					HttpListenerBasicIdentity user = Authorize (ctx);
 
-				if (!authEnabled || (user.Name.ToLower ().Equals ("admin") && user.Password.Equals (GamePrefs.GetString (EnumGamePrefs.ControlPanelPassword)))) {
-					if (ctx.Request.Url.AbsolutePath.Length < 2) {
-						handlers ["/index.htm"].HandleRequest (ctx.Request, ctx.Response, user);
-						return;
-					} else {
-						foreach (KeyValuePair<string, PathHandler> kvp in handlers) {
-							if (ctx.Request.Url.AbsolutePath.StartsWith (kvp.Key)) {
-								kvp.Value.HandleRequest (ctx.Request, ctx.Response, user);
-								return;
+					if (!authEnabled || (user.Name.ToLower ().Equals ("admin") && user.Password.Equals (GamePrefs.GetString (EnumGamePrefs.ControlPanelPassword)))) {
+						if (ctx.Request.Url.AbsolutePath.Length < 2) {
+							handlers ["/index.htm"].HandleRequest (ctx.Request, ctx.Response, user);
+							return;
+						} else {
+							foreach (KeyValuePair<string, PathHandler> kvp in handlers) {
+								if (ctx.Request.Url.AbsolutePath.StartsWith (kvp.Key)) {
+									kvp.Value.HandleRequest (ctx.Request, ctx.Response, user);
+									return;
+								}
 							}
 						}
+
+						Log.Out ("Error in Web.HandleRequest(): No handler found for path \"" + ctx.Request.Url.AbsolutePath + "\"");
+						ctx.Response.StatusCode = (int)HttpStatusCode.NotFound;
+					} else {
+						ctx.Response.StatusCode = (int)HttpStatusCode.Unauthorized;
+						ctx.Response.Headers ["WWW-Authenticate"] = "Basic realm=\"" + realm + "\"";
 					}
-
-					Log.Out ("Error in Web.HandleRequest(): No handler found for path \"" + ctx.Request.Url.AbsolutePath + "\"");
-					ctx.Response.StatusCode = (int)HttpStatusCode.NotFound;
-				} else {
-					ctx.Response.StatusCode = (int)HttpStatusCode.Unauthorized;
-					ctx.Response.Headers ["WWW-Authenticate"] = "Basic realm=\"" + realm + "\"";
+				} catch (IOException e) {
+					if (e.InnerException is SocketException) {
+						if (e.InnerException.Message.Contains ("forcibly closed") || e.InnerException.Message.Contains ("socket has been shut down"))
+							Log.Out ("Error in Web.HandleRequest(): Remote host closed connection");
+						else
+							Log.Out ("Error (IO->Socket) in Web.HandleRequest(): " + e);
+					} else {
+						Log.Out ("Error (IO) in Web.HandleRequest(): " + e);
+					}
+				} catch (Exception e) {
+					Log.Out ("Error in Web.HandleRequest(): " + e);
+				} finally {
+					if (ctx != null)
+						ctx.Response.OutputStream.Close ();
+					Interlocked.Decrement(ref currentHandlers);
 				}
-
-//				byte[] buf = Encoding.UTF8.GetBytes ("Hello World");
-//				resp.ContentLength64 = buf.Length;
-//				resp.ContentType = "text/html";
-//				resp.ContentEncoding = Encoding.UTF8;
-//				resp.OutputStream.Write (buf, 0, buf.Length);
-			} catch (Exception e) {
-				Log.Out ("Error in Web.HandleRequest(): " + e);
-			} finally {
-				ctx.Response.Close ();
 			}
 		}
