THRIFT-1412 Thrift Transport classes should manage the lifetime of objects implementing IDisposable by implementing IDisposable themselves
Patch: Joshua Garvin

git-svn-id: https://svn.apache.org/repos/asf/thrift/trunk@1325013 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/lib/csharp/src/Transport/TServerSocket.cs b/lib/csharp/src/Transport/TServerSocket.cs
index fd5c662..1ad3bd8 100644
--- a/lib/csharp/src/Transport/TServerSocket.cs
+++ b/lib/csharp/src/Transport/TServerSocket.cs
@@ -126,22 +126,36 @@
 			{
 				TcpClient result = server.AcceptTcpClient();
 				TSocket result2 = new TSocket(result);
-				result2.Timeout = clientTimeout;
-				if (useBufferedSockets)
-				{
-					TBufferedTransport result3 = new TBufferedTransport(result2);
-					return result3;
-				}
-				else
-				{
-					return result2;
-				}
-			}
-			catch (Exception ex)
-			{
-				throw new TTransportException(ex.ToString());
-			}
-		}
+        try
+        {
+          result2 = new TSocket(result);
+          result2.Timeout = clientTimeout;
+          if (useBufferedSockets)
+          {
+            TBufferedTransport result3 = new TBufferedTransport(result2);
+            return result3;
+          }
+          else
+          {
+            return result2;
+          }
+        }
+        catch (System.Exception)
+        {
+          // If a TSocket was successfully created, then let 
+          // it do proper cleanup of the TcpClient object.
+          if (result2 != null)
+            result2.Dispose();
+          else //  Otherwise, clean it up ourselves.
+            ((IDisposable)result).Dispose();
+          throw;
+        }
+      }
+      catch (Exception ex)
+      {
+        throw new TTransportException(ex.ToString());
+      }
+    }
 
 		public override void Close()
 		{