THRIFT-3000 .NET implementation has trouble with mixed IP modes
Client: C#
Patch: Jens Geyer, based on https://github.com/apache/thrift/pull/377

This closes #1167
diff --git a/lib/csharp/src/Transport/TSocket.cs b/lib/csharp/src/Transport/TSocket.cs
index cf1a440..7501e30 100644
--- a/lib/csharp/src/Transport/TSocket.cs
+++ b/lib/csharp/src/Transport/TSocket.cs
@@ -26,216 +26,216 @@
 
 namespace Thrift.Transport
 {
-    public class TSocket : TStreamTransport
-    {
-        private TcpClient client = null;
-        private string host = null;
-        private int port = 0;
-        private int timeout = 0;
+	public class TSocket : TStreamTransport
+	{
+		private TcpClient client = null;
+		private string host = null;
+		private int port = 0;
+		private int timeout = 0;
 
-        public TSocket(TcpClient client)
-        {
-            this.client = client;
+		public TSocket(TcpClient client)
+		{
+			this.client = client;
 
-            if (IsOpen)
-            {
-                inputStream = client.GetStream();
-                outputStream = client.GetStream();
-            }
-        }
+			if (IsOpen)
+			{
+				inputStream = client.GetStream();
+				outputStream = client.GetStream();
+			}
+		}
 
-        public TSocket(string host, int port)
-            : this(host, port, 0)
-        {
-        }
+		public TSocket(string host, int port)
+			: this(host, port, 0)
+		{
+		}
 
-        public TSocket(string host, int port, int timeout)
-        {
-            this.host = host;
-            this.port = port;
-            this.timeout = timeout;
+		public TSocket(string host, int port, int timeout)
+		{
+			this.host = host;
+			this.port = port;
+			this.timeout = timeout;
 
-            InitSocket();
-        }
+			InitSocket();
+		}
 
-        private void InitSocket()
-        {
-            client = new TcpClient();
-            client.ReceiveTimeout = client.SendTimeout = timeout;
-            client.Client.NoDelay = true;
-        }
+		private void InitSocket()
+		{
+			this.client = TSocketVersionizer.CreateTcpClient();
+			this.client.ReceiveTimeout = client.SendTimeout = timeout;
+			this.client.Client.NoDelay = true;
+		}
 
-        public int Timeout
-        {
-            set
-            {
-                client.ReceiveTimeout = client.SendTimeout = timeout = value;
-            }
-        }
+		public int Timeout
+		{
+			set
+			{
+				client.ReceiveTimeout = client.SendTimeout = timeout = value;
+			}
+		}
 
-        public TcpClient TcpClient
-        {
-            get
-            {
-                return client;
-            }
-        }
+		public TcpClient TcpClient
+		{
+			get
+			{
+				return client;
+			}
+		}
 
-        public string Host
-        {
-            get
-            {
-                return host;
-            }
-        }
+		public string Host
+		{
+			get
+			{
+				return host;
+			}
+		}
 
-        public int Port
-        {
-            get
-            {
-                return port;
-            }
-        }
+		public int Port
+		{
+			get
+			{
+				return port;
+			}
+		}
 
-        public override bool IsOpen
-        {
-            get
-            {
-                if (client == null)
-                {
-                    return false;
-                }
+		public override bool IsOpen
+		{
+			get
+			{
+				if (client == null)
+				{
+					return false;
+				}
 
-                return client.Connected;
-            }
-        }
+				return client.Connected;
+			}
+		}
 
-        public override void Open()
-        {
-            if (IsOpen)
-            {
-                throw new TTransportException(TTransportException.ExceptionType.AlreadyOpen, "Socket already connected");
-            }
+		public override void Open()
+		{
+			if (IsOpen)
+			{
+				throw new TTransportException(TTransportException.ExceptionType.AlreadyOpen, "Socket already connected");
+			}
 
-            if (String.IsNullOrEmpty(host))
-            {
-                throw new TTransportException(TTransportException.ExceptionType.NotOpen, "Cannot open null host");
-            }
+			if (String.IsNullOrEmpty(host))
+			{
+				throw new TTransportException(TTransportException.ExceptionType.NotOpen, "Cannot open null host");
+			}
 
-            if (port <= 0)
-            {
-                throw new TTransportException(TTransportException.ExceptionType.NotOpen, "Cannot open without port");
-            }
+			if (port <= 0)
+			{
+				throw new TTransportException(TTransportException.ExceptionType.NotOpen, "Cannot open without port");
+			}
 
-            if (client == null)
-            {
-                InitSocket();
-            }
+			if (client == null)
+			{
+				InitSocket();
+			}
 
             if( timeout == 0)            // no timeout -> infinite
-            {
-                client.Connect(host, port);
-            }
-            else                        // we have a timeout -> use it
-            {
-                ConnectHelper hlp = new ConnectHelper(client);
-                IAsyncResult asyncres = client.BeginConnect(host, port, new AsyncCallback(ConnectCallback), hlp);
-                bool bConnected = asyncres.AsyncWaitHandle.WaitOne(timeout) && client.Connected;
-                if (!bConnected)
-                {
-                    lock (hlp.Mutex)
-                    {
+			{
+				client.Connect(host, port);
+			}
+			else                        // we have a timeout -> use it
+			{
+				ConnectHelper hlp = new ConnectHelper(client);
+				IAsyncResult asyncres = client.BeginConnect(host, port, new AsyncCallback(ConnectCallback), hlp);
+				bool bConnected = asyncres.AsyncWaitHandle.WaitOne(timeout) && client.Connected;
+				if (!bConnected)
+				{
+					lock (hlp.Mutex)
+					{
                         if( hlp.CallbackDone)
-                        {
-                            asyncres.AsyncWaitHandle.Close();
-                            client.Close();
-                        }
-                        else
-                        {
-                            hlp.DoCleanup = true;
-                            client = null;
-                        }
-                    }
-                    throw new TTransportException(TTransportException.ExceptionType.TimedOut, "Connect timed out");
-                }
-            }
+						{
+							asyncres.AsyncWaitHandle.Close();
+							client.Close();
+						}
+						else
+						{
+							hlp.DoCleanup = true;
+							client = null;
+						}
+					}
+					throw new TTransportException(TTransportException.ExceptionType.TimedOut, "Connect timed out");
+				}
+			}
 
-            inputStream = client.GetStream();
-            outputStream = client.GetStream();
-        }
+			inputStream = client.GetStream();
+			outputStream = client.GetStream();
+		}
 
 
-        static void ConnectCallback(IAsyncResult asyncres)
-        {
-            ConnectHelper hlp = asyncres.AsyncState as ConnectHelper;
-            lock (hlp.Mutex)
-            {
-                hlp.CallbackDone = true;
+		static void ConnectCallback(IAsyncResult asyncres)
+		{
+			ConnectHelper hlp = asyncres.AsyncState as ConnectHelper;
+			lock (hlp.Mutex)
+			{
+				hlp.CallbackDone = true;
 
-                try
-                {
+				try
+				{
                     if( hlp.Client.Client != null)
-                        hlp.Client.EndConnect(asyncres);
-                }
-                catch (Exception)
-                {
-                    // catch that away
-                }
+						hlp.Client.EndConnect(asyncres);
+				}
+				catch (Exception)
+				{
+					// catch that away
+				}
 
-                if (hlp.DoCleanup)
-                {
+				if (hlp.DoCleanup)
+				{
                     try {
-                        asyncres.AsyncWaitHandle.Close();
+						asyncres.AsyncWaitHandle.Close();
                     } catch (Exception) {}
 
                     try {
-                        if (hlp.Client is IDisposable)
-                            ((IDisposable)hlp.Client).Dispose();
+						if (hlp.Client is IDisposable)
+							((IDisposable)hlp.Client).Dispose();
                     } catch (Exception) {}
-                    hlp.Client = null;
-                }
-            }
-        }
+					hlp.Client = null;
+				}
+			}
+		}
 
-        private class ConnectHelper
-        {
-            public object Mutex = new object();
-            public bool DoCleanup = false;
-            public bool CallbackDone = false;
-            public TcpClient Client;
-            public ConnectHelper(TcpClient client)
-            {
-                Client = client;
-            }
-        }
+		private class ConnectHelper
+		{
+			public object Mutex = new object();
+			public bool DoCleanup = false;
+			public bool CallbackDone = false;
+			public TcpClient Client;
+			public ConnectHelper(TcpClient client)
+			{
+				Client = client;
+			}
+		}
 
-        public override void Close()
-        {
-            base.Close();
-            if (client != null)
-            {
-                client.Close();
-                client = null;
-            }
-        }
+		public override void Close()
+		{
+			base.Close();
+			if (client != null)
+			{
+				client.Close();
+				client = null;
+			}
+		}
 
-    #region " IDisposable Support "
-    private bool _IsDisposed;
+		#region " IDisposable Support "
+		private bool _IsDisposed;
 
-    // IDisposable
-    protected override void Dispose(bool disposing)
-    {
-      if (!_IsDisposed)
-      {
-        if (disposing)
-        {
-          if (client != null)
-            ((IDisposable)client).Dispose();
-          base.Dispose(disposing);
-        }
-      }
-      _IsDisposed = true;
-    }
-    #endregion
-  }
+		// IDisposable
+		protected override void Dispose(bool disposing)
+		{
+			if (!_IsDisposed)
+			{
+				if (disposing)
+				{
+					if (client != null)
+						((IDisposable)client).Dispose();
+					base.Dispose(disposing);
+				}
+			}
+			_IsDisposed = true;
+		}
+		#endregion
+	}
 }