Merge pull request #3066 from alexanderkyte/pedump_sgen
[mono.git] / mcs / class / System / Test / System.Net.Sockets / SocketTest.cs
index 1f18e4705d0a176a08e3d422b0fbd480cbeb3370..2b12d7019bffad7b8ebc05bd816b8e773a684871 100755 (executable)
@@ -9,16 +9,20 @@
 //
 
 using System;
+using System.Diagnostics;
+using System.Linq;
 using System.Collections;
 using System.Threading;
+using System.Reflection;
+using System.Text.RegularExpressions;
 using System.Net;
 using System.Net.Sockets;
 using NUnit.Framework;
 using System.IO;
 
-#if NET_2_0
 using System.Collections.Generic;
-#endif
+
+using MonoTests.Helpers;
 
 namespace MonoTests.System.Net.Sockets
 {
@@ -32,7 +36,7 @@ namespace MonoTests.System.Net.Sockets
                [Test]
                public void ConnectIPAddressAny ()
                {
-                       IPEndPoint ep = new IPEndPoint (IPAddress.Any, 0);
+                       IPEndPoint ep = new IPEndPoint (IPAddress.Any, NetworkHelpers.FindFreePort ());
 
                        /* UDP sockets use Any to disconnect
                        try {
@@ -62,7 +66,7 @@ namespace MonoTests.System.Net.Sockets
                public void IncompatibleAddress ()
                {
                        IPEndPoint epIPv6 = new IPEndPoint (IPAddress.IPv6Any,
-                                                               0);
+                                                               NetworkHelpers.FindFreePort ());
 
                        try {
                                using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP)) {
@@ -71,13 +75,8 @@ namespace MonoTests.System.Net.Sockets
                                }
                                Assert.Fail ("#1");
                        } catch (SocketException ex) {
-#if !NET_2_0
-                               // invalid argument
-                               int expectedError = 10022;
-#else
                                // address incompatible with protocol
                                int expectedError = 10047;
-#endif
                                Assert.AreEqual (expectedError, ex.ErrorCode,
                                                "#2");
                        }
@@ -114,9 +113,9 @@ namespace MonoTests.System.Net.Sockets
                        Socket.Select (list, list, list, 1000);
                }
                
-               private bool BlockingConnect (bool block)
+               private bool BlockingConnect (bool block, int port)
                {
-                       IPEndPoint ep = new IPEndPoint(IPAddress.Loopback, 1234);
+                       IPEndPoint ep = new IPEndPoint(IPAddress.Loopback, port);
                        Socket server = new Socket(AddressFamily.InterNetwork,
                                                   SocketType.Stream,
                                                   ProtocolType.Tcp);
@@ -130,7 +129,21 @@ namespace MonoTests.System.Net.Sockets
                                                  ProtocolType.Tcp);
                        conn.Connect (ep);
 
-                       Socket client = server.Accept();
+                       Socket client = null;
+                       var sw = Stopwatch.StartNew ();
+                       while (sw.ElapsedMilliseconds < 100)
+                       {
+                               try {
+                                       client = server.Accept();
+                                       break;
+                               }
+                               catch (SocketException ex) {
+                                       if (ex.SocketErrorCode == SocketError.WouldBlock)
+                                               continue;
+                                       throw;
+                               }
+                       }
+                       Assert.IsNotNull (client, "Couldn't accept a client connection within 100ms.");
                        bool client_block = client.Blocking;
 
                        client.Close();
@@ -144,11 +157,12 @@ namespace MonoTests.System.Net.Sockets
                public void AcceptBlockingStatus()
                {
                        bool block;
-
-                       block = BlockingConnect(true);
+                       var port = NetworkHelpers.FindFreePort ();
+       
+                       block = BlockingConnect(true, port);
                        Assert.AreEqual (block, true, "BlockingStatus01");
 
-                       block = BlockingConnect(false);
+                       block = BlockingConnect(false, port);
                        Assert.AreEqual (block, false, "BlockingStatus02");
                }
 
@@ -197,7 +211,7 @@ namespace MonoTests.System.Net.Sockets
                         * anything...
                         */
                        sock.BeginConnect (new IPEndPoint (IPAddress.Loopback,
-                                                          114),
+                                                          NetworkHelpers.FindFreePort ()),
                                           new AsyncCallback (CFACallback),
                                           sock);
                        CFACalledBack.WaitOne ();
@@ -206,12 +220,9 @@ namespace MonoTests.System.Net.Sockets
                }
                
                [Test]
-#if !NET_2_0
-               [ExpectedException (typeof (ArgumentException))]
-#endif
                public void SetSocketOptionBoolean ()
                {
-                       IPEndPoint ep = new IPEndPoint (IPAddress.Loopback, 1);
+                       IPEndPoint ep = new IPEndPoint (IPAddress.Loopback, NetworkHelpers.FindFreePort ());
                        Socket sock = new Socket (ep.Address.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
                        try {
                                sock.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);
@@ -222,7 +233,7 @@ namespace MonoTests.System.Net.Sockets
                [Test]
                public void TestSelect1 ()
                {
-                       Socket srv = CreateServer ();
+                       Socket srv = CreateServer (NetworkHelpers.FindFreePort ());
                        ClientSocket clnt = new ClientSocket (srv.LocalEndPoint);
                        Thread th = new Thread (new ThreadStart (clnt.ConnectSleepClose));
                        Socket acc = null;
@@ -252,10 +263,10 @@ namespace MonoTests.System.Net.Sockets
                        }
                }
 
-               static Socket CreateServer ()
+               static Socket CreateServer (int port)
                {
                        Socket sock = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
-                       sock.Bind (new IPEndPoint (IPAddress.Loopback, 0));
+                       sock.Bind (new IPEndPoint (IPAddress.Loopback, port));
                        sock.Listen (1);
                        return sock;
                }
@@ -361,7 +372,7 @@ namespace MonoTests.System.Net.Sockets
                public void Disposed19 ()
                {
                        Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
-                       EndPoint ep = new IPEndPoint (IPAddress.Any, 31337);
+                       EndPoint ep = new IPEndPoint (IPAddress.Any, NetworkHelpers.FindFreePort ());
                        s.Close();
 
                        s.SendTo (buf, 0, ep);
@@ -372,7 +383,7 @@ namespace MonoTests.System.Net.Sockets
                public void Disposed20 ()
                {
                        Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
-                       EndPoint ep = new IPEndPoint (IPAddress.Any, 31337);
+                       EndPoint ep = new IPEndPoint (IPAddress.Any, NetworkHelpers.FindFreePort ());
                        s.Close();
 
                        s.SendTo (buf, 10, 0, ep);
@@ -383,7 +394,7 @@ namespace MonoTests.System.Net.Sockets
                public void Disposed21 ()
                {
                        Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
-                       EndPoint ep = new IPEndPoint (IPAddress.Any, 31337);
+                       EndPoint ep = new IPEndPoint (IPAddress.Any, NetworkHelpers.FindFreePort ());
                        s.Close();
 
                        s.SendTo (buf, 0, 10, 0, ep);
@@ -394,7 +405,7 @@ namespace MonoTests.System.Net.Sockets
                public void Disposed22 ()
                {
                        Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
-                       EndPoint ep = new IPEndPoint (IPAddress.Any, 31337);
+                       EndPoint ep = new IPEndPoint (IPAddress.Any, NetworkHelpers.FindFreePort ());
                        s.Close();
 
                        s.SendTo (buf, ep);
@@ -416,7 +427,7 @@ namespace MonoTests.System.Net.Sockets
                        Socket server = new Socket (AddressFamily.InterNetwork,
                                SocketType.Stream, ProtocolType.Tcp);
                        IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
-                                                       9010);
+                                                       NetworkHelpers.FindFreePort ());
                        server.Bind (ep);
                        server.Listen (1);
 
@@ -428,11 +439,7 @@ namespace MonoTests.System.Net.Sockets
                        Assert.AreEqual (hashcodeA, hashcodeB, "#1");
                        client.Close ();
                        int hashcodeC = client.GetHashCode ();
-#if NET_2_0
                        Assert.AreEqual (hashcodeB, hashcodeC, "#2");
-#else
-                       Assert.IsFalse (hashcodeB == hashcodeC, "#2");
-#endif
                        server.Close ();
                }
 
@@ -482,7 +489,6 @@ namespace MonoTests.System.Net.Sockets
                }
                
 
-#if NET_2_0
                [Test]
                public void SocketInformationCtor ()
                {
@@ -567,8 +573,6 @@ namespace MonoTests.System.Net.Sockets
                                sock.DontFragment = true;
                                Assert.Fail ("DontFragment #1");
                        } catch (NotSupportedException) {
-                       } catch {
-                               Assert.Fail ("DontFragment #2");
                        } finally {
                                sock.Close ();
                        }
@@ -586,8 +590,6 @@ namespace MonoTests.System.Net.Sockets
                                Assert.Fail ("EnableBroadcastDefaultTcp #1");
                        } catch (SocketException ex) {
                                Assert.AreEqual (10042, ex.ErrorCode, "EnableBroadcastDefaultTcp #2");
-                       } catch {
-                               Assert.Fail ("EnableBroadcastDefaultTcp #2");
                        } finally {
                                sock.Close ();
                        }
@@ -617,8 +619,6 @@ namespace MonoTests.System.Net.Sockets
                                Assert.Fail ("EnableBroadcastChangeTcp #1");
                        } catch (SocketException ex) {
                                Assert.AreEqual (10042, ex.ErrorCode, "EnableBroadcastChangeTcp #2");
-                       } catch {
-                               Assert.Fail ("EnableBroadcastChangeTcp #2");
                        } finally {
                                sock.Close ();
                        }
@@ -679,7 +679,7 @@ namespace MonoTests.System.Net.Sockets
                                                  SocketType.Stream,
                                                  ProtocolType.Tcp);
                        
-                       sock.Bind (new IPEndPoint (IPAddress.Any, 1235));
+                       sock.Bind (new IPEndPoint (IPAddress.Any, NetworkHelpers.FindFreePort ()));
                        sock.ExclusiveAddressUse = true;
                        sock.Close ();
                }
@@ -787,8 +787,6 @@ namespace MonoTests.System.Net.Sockets
                                Assert.Fail ("MulticastLoopbackDefaultTcp #1");
                        } catch (SocketException ex) {
                                Assert.AreEqual (10042, ex.ErrorCode, "MulticastLoopbackDefaultTcp #2");
-                       } catch {
-                               Assert.Fail ("MulticastLoopbackDefaultTcp #2");
                        } finally {
                                sock.Close ();
                        }
@@ -806,8 +804,6 @@ namespace MonoTests.System.Net.Sockets
                                Assert.Fail ("MulticastLoopbackChangeTcp #1");
                        } catch (SocketException ex) {
                                Assert.AreEqual (10042, ex.ErrorCode, "MulticastLoopbackChangeTcp #2");
-                       } catch {
-                               Assert.Fail ("MulticastLoopbackChangeTcp #2");
                        } finally {
                                sock.Close ();
                        }
@@ -991,7 +987,6 @@ namespace MonoTests.System.Net.Sockets
                }
 
                [Test]
-               [Category ("NotOnMac")] // Mac doesn't throw when overflowing the ttl
                public void TtlChangeOverflow ()
                {
                        Socket sock = new Socket (AddressFamily.InterNetwork,
@@ -1001,11 +996,9 @@ namespace MonoTests.System.Net.Sockets
                        try {
                                sock.Ttl = 256;
                                Assert.Fail ("TtlChangeOverflow #1");
-                       } catch (SocketException ex) {
-                               Assert.AreEqual (10022, ex.ErrorCode,
+                       } catch (ArgumentOutOfRangeException ex) {
+                               Assert.AreEqual ("value", ex.ParamName,
                                                 "TtlChangeOverflow #2");
-                       } catch {
-                               Assert.Fail ("TtlChangeoverflow #3");
                        } finally {
                                sock.Close ();
                        }
@@ -1018,8 +1011,6 @@ namespace MonoTests.System.Net.Sockets
                        } catch (SocketException ex) {
                                Assert.AreEqual (10022, ex.ErrorCode,
                                                 "TtlChangeOverflow #5");
-                       } catch {
-                               Assert.Fail ("TtlChangeOverflow #6");
                        } finally {
                                sock.Close ();
                        }
@@ -1133,8 +1124,6 @@ namespace MonoTests.System.Net.Sockets
                                sock.SendTimeout = -2;
                                Assert.Fail ("SendTimeoutChange #8");
                        } catch (ArgumentOutOfRangeException) {
-                       } catch {
-                               Assert.Fail ("SendTimeoutChange #9");
                        } finally {
                                sock.Close ();
                        }
@@ -1192,8 +1181,6 @@ namespace MonoTests.System.Net.Sockets
                                sock.ReceiveTimeout = -2;
                                Assert.Fail ("ReceiveTimeoutChange #8");
                        } catch (ArgumentOutOfRangeException) {
-                       } catch {
-                               Assert.Fail ("ReceiveTimeoutChange #9");
                        } finally {
                                sock.Close ();
                        }
@@ -1251,8 +1238,6 @@ namespace MonoTests.System.Net.Sockets
                        } catch (SocketException ex) {
                                Assert.AreEqual (10042, ex.ErrorCode,
                                                 "NoDelayDefaultUdp #2");
-                       } catch {
-                               Assert.Fail ("NoDelayDefaultUdp #3");
                        } finally {
                                sock.Close ();
                        }
@@ -1271,8 +1256,6 @@ namespace MonoTests.System.Net.Sockets
                        } catch (SocketException ex) {
                                Assert.AreEqual (10042, ex.ErrorCode,
                                                 "NoDelayChangeUdp #2");
-                       } catch {
-                               Assert.Fail ("NoDelayChangeUdp #3");
                        } finally {
                                sock.Close ();
                        }
@@ -1326,7 +1309,7 @@ namespace MonoTests.System.Net.Sockets
                                                  SocketType.Stream,
                                                  ProtocolType.Tcp);
 
-                       sock.Bind (new IPEndPoint (IPAddress.Any, 1236));
+                       sock.Bind (new IPEndPoint (IPAddress.Any, NetworkHelpers.FindFreePort ()));
                        
                        sock.BeginAccept (BACallback, sock);
                        
@@ -1340,7 +1323,7 @@ namespace MonoTests.System.Net.Sockets
                                                  SocketType.Stream,
                                                  ProtocolType.Tcp);
                        IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
-                                                       1237);
+                                                       NetworkHelpers.FindFreePort ());
                        
                        sock.Bind (ep);
                        sock.Listen (1);
@@ -1407,7 +1390,7 @@ namespace MonoTests.System.Net.Sockets
                                                  SocketType.Stream,
                                                  ProtocolType.Tcp);
                        IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
-                                                       1238);
+                                                       NetworkHelpers.FindFreePort ());
                        
                        sock.Bind (ep);
                        sock.Listen (1);
@@ -1475,7 +1458,7 @@ namespace MonoTests.System.Net.Sockets
                                                 ProtocolType.Udp);
                        
                        IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
-                                                       1239);
+                                                       NetworkHelpers.FindFreePort ());
                        
                        sock.Bind (ep);
                        sock.Listen (1);
@@ -1485,8 +1468,6 @@ namespace MonoTests.System.Net.Sockets
                                Assert.Fail ("BeginAcceptSocketUdp #1");
                        } catch (SocketException ex) {
                                Assert.AreEqual (10022, ex.ErrorCode, "BeginAcceptSocketUdp #2");
-                       } catch {
-                               Assert.Fail ("BeginAcceptSocketUdp #3");
                        } finally {
                                acc.Close ();
                                sock.Close ();
@@ -1504,10 +1485,10 @@ namespace MonoTests.System.Net.Sockets
                                                 ProtocolType.Tcp);
                        
                        IPEndPoint ep1 = new IPEndPoint (IPAddress.Loopback,
-                                                        1240);
+                                                        NetworkHelpers.FindFreePort ());
                        
                        IPEndPoint ep2 = new IPEndPoint (IPAddress.Loopback,
-                                                        1241);
+                                                        NetworkHelpers.FindFreePort ());
                        
                        sock.Bind (ep1);
                        sock.Listen (1);
@@ -1518,8 +1499,6 @@ namespace MonoTests.System.Net.Sockets
                                sock.BeginAccept (acc, 256, BADCallback, sock);
                                Assert.Fail ("BeginAcceptSocketBound #1");
                        } catch (InvalidOperationException) {
-                       } catch {
-                               Assert.Fail ("BeginAcceptSocketBound #2");
                        } finally {
                                acc.Close ();
                                sock.Close ();
@@ -1537,7 +1516,7 @@ namespace MonoTests.System.Net.Sockets
                                                 ProtocolType.Tcp);
                        
                        IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
-                                                       1242);
+                                                       NetworkHelpers.FindFreePort ());
                        
                        sock.Bind (ep);
                        sock.Listen (1);
@@ -1602,8 +1581,6 @@ namespace MonoTests.System.Net.Sockets
                                sock.BeginAccept (acc, 256, BADCallback, null);
                                Assert.Fail ("BeginAcceptSocketClosed #1");
                        } catch (ObjectDisposedException) {
-                       } catch {
-                               Assert.Fail ("BeginAcceptSocketClosed #2");
                        } finally {
                                acc.Close ();
                        }
@@ -1619,7 +1596,7 @@ namespace MonoTests.System.Net.Sockets
                                                 SocketType.Stream,
                                                 ProtocolType.Tcp);
                        IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
-                                                       1243);
+                                                       NetworkHelpers.FindFreePort ());
 
                        sock.Bind (ep);
                        sock.Listen (1);
@@ -1632,8 +1609,6 @@ namespace MonoTests.System.Net.Sockets
                                sock.BeginAccept (acc, 256, BADCallback, null);
                                Assert.Fail ("BeginAcceptSocketAccClosed #1");
                        } catch (ObjectDisposedException) {
-                       } catch {
-                               Assert.Fail ("BeginAcceptSocketAccClosed #2");
                        } finally {
                                sock.Close ();
                        }
@@ -1670,7 +1645,7 @@ namespace MonoTests.System.Net.Sockets
                                                    SocketType.Stream,
                                                    ProtocolType.Tcp);
                        IPAddress ip = IPAddress.Loopback;
-                       IPEndPoint ep = new IPEndPoint (ip, 1244);
+                       IPEndPoint ep = new IPEndPoint (ip, NetworkHelpers.FindFreePort ());
 
                        listen.Bind (ep);
                        listen.Listen (1);
@@ -1679,7 +1654,7 @@ namespace MonoTests.System.Net.Sockets
                        
                        BCConnected = false;
                        
-                       sock.BeginConnect (ip, 1244, BCCallback, sock);
+                       sock.BeginConnect (ip, ep.Port, BCCallback, sock);
 
                        if (BCCalledBack.WaitOne (2000, false) == false) {
                                Assert.Fail ("BeginConnectAddressPort wait timed out");
@@ -1704,8 +1679,6 @@ namespace MonoTests.System.Net.Sockets
                                                   sock);
                                Assert.Fail ("BeginConnectAddressPortNull #1");
                        } catch (ArgumentNullException) {
-                       } catch {
-                               Assert.Fail ("BeginConnectAddressPortNull #2");
                        } finally {
                                sock.Close ();
                        }
@@ -1718,17 +1691,15 @@ namespace MonoTests.System.Net.Sockets
                                                  SocketType.Stream,
                                                  ProtocolType.Tcp);
                        IPAddress ip = IPAddress.Loopback;
-                       IPEndPoint ep = new IPEndPoint (ip, 1245);
+                       IPEndPoint ep = new IPEndPoint (ip, NetworkHelpers.FindFreePort ());
 
                        sock.Bind (ep);
                        sock.Listen (1);
                        
                        try {
-                               sock.BeginConnect (ip, 1245, BCCallback, sock);
+                               sock.BeginConnect (ip, ep.Port, BCCallback, sock);
                                Assert.Fail ("BeginConnectAddressPortListen #1");
                        } catch (InvalidOperationException) {
-                       } catch {
-                               Assert.Fail ("BeginConnectAddressPortListen #2");
                        } finally {
                                sock.Close ();
                        }
@@ -1769,7 +1740,7 @@ namespace MonoTests.System.Net.Sockets
                                                    SocketType.Stream,
                                                    ProtocolType.Tcp);
                        IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
-                                                       1246);
+                                                       NetworkHelpers.FindFreePort ());
                        IPAddress[] ips = new IPAddress[4];
                        
                        ips[0] = IPAddress.Parse ("127.0.0.4");
@@ -1784,7 +1755,7 @@ namespace MonoTests.System.Net.Sockets
                        
                        BCConnected = false;
                        
-                       sock.BeginConnect (ips, 1246, BCCallback, sock);
+                       sock.BeginConnect (ips, ep.Port, BCCallback, sock);
                        
                        /* Longer wait here, because the ms runtime
                         * takes a lot longer to not connect
@@ -1795,16 +1766,7 @@ namespace MonoTests.System.Net.Sockets
                        }
                        */
 
-                       var sw = new global::System.Diagnostics.Stopwatch ();
-                       sw.Start ();
-
-                       BCCalledBack.WaitOne ();
-
-                       sw.Stop ();
-                       Console.WriteLine (sw.ElapsedMilliseconds);
-
-                       if (sw.ElapsedMilliseconds > 30000)
-                               Assert.Fail ("BeginConnectMultiple wait failed");
+                       Assert.IsTrue (BCCalledBack.WaitOne (30000), "#0");
                        
                        Assert.AreEqual (true, BCConnected, "BeginConnectMultiple #1");
                        Assert.AreEqual (AddressFamily.InterNetwork, sock.RemoteEndPoint.AddressFamily, "BeginConnectMultiple #2");
@@ -1841,7 +1803,7 @@ namespace MonoTests.System.Net.Sockets
                         * succeed it it can connect to at least one of the requested
                         * addresses.
                         */
-                       IPEndPoint ep = new IPEndPoint (IPAddress.Loopback, 1246);
+                       IPEndPoint ep = new IPEndPoint (IPAddress.Loopback, NetworkHelpers.FindFreePort ());
 
                        listen.Bind (ep);
                        listen.Listen (1);
@@ -1850,7 +1812,7 @@ namespace MonoTests.System.Net.Sockets
                        
                        BCConnected = false;
                        
-                       sock.BeginConnect (allIps, 1246, BCCallback, sock);
+                       sock.BeginConnect (allIps, ep.Port, BCCallback, sock);
                        
                        /* Longer wait here, because the ms runtime
                         * takes a lot longer to not connect
@@ -1883,8 +1845,6 @@ namespace MonoTests.System.Net.Sockets
                                                   sock);
                                Assert.Fail ("BeginConnectMultipleNull #1");
                        } catch (ArgumentNullException) {
-                       } catch {
-                               Assert.Fail ("BeginConnectMultipleNull #2");
                        } finally {
                                sock.Close ();
                        }
@@ -1898,7 +1858,7 @@ namespace MonoTests.System.Net.Sockets
                                                  ProtocolType.Tcp);
                        IPAddress[] ips = new IPAddress[4];
                        IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
-                                                       1247);
+                                                       NetworkHelpers.FindFreePort ());
                        
                        ips[0] = IPAddress.Parse ("127.0.0.4");
                        ips[1] = IPAddress.Parse ("127.0.0.3");
@@ -1909,12 +1869,10 @@ namespace MonoTests.System.Net.Sockets
                        sock.Listen (1);
                        
                        try {
-                               sock.BeginConnect (ips, 1247, BCCallback,
+                               sock.BeginConnect (ips, ep.Port, BCCallback,
                                                   sock);
                                Assert.Fail ("BeginConnectMultipleListen #1");
                        } catch (InvalidOperationException) {
-                       } catch {
-                               Assert.Fail ("BeginConnectMultipleListen #2");
                        } finally {
                                sock.Close ();
                        }
@@ -1951,8 +1909,6 @@ namespace MonoTests.System.Net.Sockets
                                                   BCCallback, sock);
                                Assert.Fail ("BeginConnectHostPort #1");
                        } catch (ArgumentNullException) {
-                       } catch {
-                               Assert.Fail ("BeginConnectHostPort #2");
                        } finally {
                                sock.Close ();
                        }
@@ -1965,18 +1921,16 @@ namespace MonoTests.System.Net.Sockets
                                                  SocketType.Stream,
                                                  ProtocolType.Tcp);
                        IPAddress ip = IPAddress.Loopback;
-                       IPEndPoint ep = new IPEndPoint (ip, 1248);
+                       IPEndPoint ep = new IPEndPoint (ip, NetworkHelpers.FindFreePort ());
                        
                        sock.Bind (ep);
                        sock.Listen (1);
                        
                        try {
-                               sock.BeginConnect ("localhost", 1248,
+                               sock.BeginConnect ("localhost", ep.Port,
                                                   BCCallback, sock);
                                Assert.Fail ("BeginConnectHostPortListen #1");
                        } catch (InvalidOperationException) {
-                       } catch {
-                               Assert.Fail ("BeginConnectHostPortListen #2");
                        } finally {
                                sock.Close ();
                        }
@@ -1995,8 +1949,6 @@ namespace MonoTests.System.Net.Sockets
                                                   sock);
                                Assert.Fail ("BeginConnectHostPortNotIP #1");
                        } catch (NotSupportedException) {
-                       } catch {
-                               Assert.Fail ("BeginConnectHostPortNotIP #2");
                        } finally {
                                sock.Close ();
                        }
@@ -2039,12 +1991,12 @@ namespace MonoTests.System.Net.Sockets
                                                    SocketType.Stream,
                                                    ProtocolType.Tcp);
                        IPAddress ip = IPAddress.Loopback;
-                       IPEndPoint ep = new IPEndPoint (ip, 1254);
+                       IPEndPoint ep = new IPEndPoint (ip, NetworkHelpers.FindFreePort ());
                        
                        listen.Bind (ep);
                        listen.Listen (1);
                        
-                       sock.Connect (ip, 1254);
+                       sock.Connect (ip, ep.Port);
                        
                        Assert.AreEqual (true, sock.Connected, "BeginDisconnect #1");
                        
@@ -2105,8 +2057,6 @@ namespace MonoTests.System.Net.Sockets
                                Assert.Fail ("BeginSendNotConnected #1");
                        } catch (SocketException ex) {
                                Assert.AreEqual (10057, ex.ErrorCode, "BeginSendNotConnected #2");
-                       } catch {
-                               Assert.Fail ("BeginSendNotConnected #3");
                        } finally {
                                sock.Close ();
                        }
@@ -2134,9 +2084,9 @@ namespace MonoTests.System.Net.Sockets
                                                  SocketType.Stream,
                                                  ProtocolType.Tcp);
                        IPEndPoint ep1 = new IPEndPoint (IPAddress.Loopback,
-                                                       1256);
+                                                       NetworkHelpers.FindFreePort ());
                        IPEndPoint ep2 = new IPEndPoint (IPAddress.Loopback,
-                                                        1257);
+                                                        NetworkHelpers.FindFreePort ());
                        
                        sock.Bind (ep1);
                        
@@ -2145,8 +2095,6 @@ namespace MonoTests.System.Net.Sockets
                                Assert.Fail ("BindTwice #1");
                        } catch (SocketException ex) {
                                Assert.AreEqual (10022, ex.ErrorCode, "BindTwice #2");
-                       } catch {
-                               Assert.Fail ("BindTwice #3");
                        } finally {
                                sock.Close ();
                        }
@@ -2162,7 +2110,7 @@ namespace MonoTests.System.Net.Sockets
                                                    SocketType.Stream,
                                                    ProtocolType.Tcp);
                        IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
-                                                       1258);
+                                                       NetworkHelpers.FindFreePort ());
                        
                        listen.Bind (ep);
                        listen.Listen (1);
@@ -2190,12 +2138,12 @@ namespace MonoTests.System.Net.Sockets
                                                    SocketType.Stream,
                                                    ProtocolType.Tcp);
                        IPAddress ip = IPAddress.Loopback;
-                       IPEndPoint ep = new IPEndPoint (ip, 1249);
+                       IPEndPoint ep = new IPEndPoint (ip, NetworkHelpers.FindFreePort ());
 
                        listen.Bind (ep);
                        listen.Listen (1);
                        
-                       sock.Connect (ip, 1249);
+                       sock.Connect (ip, ep.Port);
                        
                        Assert.AreEqual (true, sock.Connected, "ConnectAddressPort #1");
                        
@@ -2215,8 +2163,6 @@ namespace MonoTests.System.Net.Sockets
                                sock.Connect (ip, 1249);
                                Assert.Fail ("ConnectAddressPortNull #1");
                        } catch (ArgumentNullException) {
-                       } catch {
-                               Assert.Fail ("ConnectAddressPortNull #2");
                        } finally {
                                sock.Close ();
                        }
@@ -2229,17 +2175,15 @@ namespace MonoTests.System.Net.Sockets
                                                  SocketType.Stream,
                                                  ProtocolType.Tcp);
                        IPAddress ip = IPAddress.Loopback;
-                       IPEndPoint ep = new IPEndPoint (ip, 1250);
+                       IPEndPoint ep = new IPEndPoint (ip, NetworkHelpers.FindFreePort ());
 
                        sock.Bind (ep);
                        sock.Listen (1);
                        
                        try {
-                               sock.Connect (ip, 1250);
+                               sock.Connect (ip, ep.Port);
                                Assert.Fail ("ConnectAddressPortListen #1");
                        } catch (InvalidOperationException) {
-                       } catch {
-                               Assert.Fail ("ConnectAddressPortListen #2");
                        } finally {
                                sock.Close ();
                        }
@@ -2287,7 +2231,7 @@ namespace MonoTests.System.Net.Sockets
                                                    SocketType.Stream,
                                                    ProtocolType.Tcp);
                        IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
-                                                       1251);
+                                                       NetworkHelpers.FindFreePort ());
                        IPAddress[] ips = new IPAddress[4];
                        
                        ips[0] = IPAddress.Parse ("127.0.0.4");
@@ -2298,7 +2242,7 @@ namespace MonoTests.System.Net.Sockets
                        listen.Bind (ep);
                        listen.Listen (1);
                        
-                       sock.Connect (ips, 1251);
+                       sock.Connect (ips, ep.Port);
                        
                        Assert.AreEqual (true, sock.Connected, "ConnectMultiple #1");
                        Assert.AreEqual (AddressFamily.InterNetwork, sock.RemoteEndPoint.AddressFamily, "ConnectMultiple #2");
@@ -2333,12 +2277,12 @@ namespace MonoTests.System.Net.Sockets
                         * Bind to IPAddress.Any; Connect() will fail unless it can
                         * connect to all the addresses in allIps.
                         */
-                       IPEndPoint ep = new IPEndPoint (IPAddress.Any, 1251);
+                       IPEndPoint ep = new IPEndPoint (IPAddress.Any, NetworkHelpers.FindFreePort ());
 
                        listen.Bind (ep);
                        listen.Listen (1);
                        
-                       sock.Connect (allIps, 1251);
+                       sock.Connect (allIps, ep.Port);
                        
                        Assert.AreEqual (true, sock.Connected, "ConnectMultiple2 #1");
                        Assert.AreEqual (AddressFamily.InterNetwork, sock.RemoteEndPoint.AddressFamily, "ConnectMultiple2 #2");
@@ -2362,8 +2306,6 @@ namespace MonoTests.System.Net.Sockets
                                sock.Connect (ips, 1251);
                                Assert.Fail ("ConnectMultipleNull #1");
                        } catch (ArgumentNullException) {
-                       } catch {
-                               Assert.Fail ("ConnectMultipleNull #2");
                        } finally {
                                sock.Close ();
                        }
@@ -2377,7 +2319,7 @@ namespace MonoTests.System.Net.Sockets
                                                  ProtocolType.Tcp);
                        IPAddress[] ips = new IPAddress[4];
                        IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
-                                                       1252);
+                                                       NetworkHelpers.FindFreePort ());
                        
                        ips[0] = IPAddress.Parse ("127.0.0.4");
                        ips[1] = IPAddress.Parse ("127.0.0.3");
@@ -2388,11 +2330,9 @@ namespace MonoTests.System.Net.Sockets
                        sock.Listen (1);
                        
                        try {
-                               sock.Connect (ips, 1252);
+                               sock.Connect (ips, ep.Port);
                                Assert.Fail ("ConnectMultipleListen #1");
                        } catch (InvalidOperationException) {
-                       } catch {
-                               Assert.Fail ("ConnectMultipleListen #2");
                        } finally {
                                sock.Close ();
                        }
@@ -2428,8 +2368,6 @@ namespace MonoTests.System.Net.Sockets
                                sock.Connect ((string)null, 0);
                                Assert.Fail ("ConnectHostPort #1");
                        } catch (ArgumentNullException) {
-                       } catch {
-                               Assert.Fail ("ConnectHostPort #2");
                        } finally {
                                sock.Close ();
                        }
@@ -2442,17 +2380,15 @@ namespace MonoTests.System.Net.Sockets
                                                  SocketType.Stream,
                                                  ProtocolType.Tcp);
                        IPAddress ip = IPAddress.Loopback;
-                       IPEndPoint ep = new IPEndPoint (ip, 1253);
+                       IPEndPoint ep = new IPEndPoint (ip, NetworkHelpers.FindFreePort ());
                        
                        sock.Bind (ep);
                        sock.Listen (1);
                        
                        try {
-                               sock.Connect ("localhost", 1253);
+                               sock.Connect ("localhost", ep.Port);
                                Assert.Fail ("ConnectHostPortListen #1");
                        } catch (InvalidOperationException) {
-                       } catch {
-                               Assert.Fail ("ConnectHostPortListen #2");
                        } finally {
                                sock.Close ();
                        }
@@ -2470,8 +2406,6 @@ namespace MonoTests.System.Net.Sockets
                                sock.Connect ("localhost", 0);
                                Assert.Fail ("ConnectHostPortNotIP #1");
                        } catch (NotSupportedException) {
-                       } catch {
-                               Assert.Fail ("ConnectHostPortNotIP #2");
                        } finally {
                                sock.Close ();
                        }
@@ -2501,12 +2435,12 @@ namespace MonoTests.System.Net.Sockets
                                                    SocketType.Stream,
                                                    ProtocolType.Tcp);
                        IPAddress ip = IPAddress.Loopback;
-                       IPEndPoint ep = new IPEndPoint (ip, 1255);
+                       IPEndPoint ep = new IPEndPoint (ip, NetworkHelpers.FindFreePort ());
                        
                        listen.Bind (ep);
                        listen.Listen (1);
                        
-                       sock.Connect (ip, 1255);
+                       sock.Connect (ip, ep.Port);
                        
                        Assert.AreEqual (true, sock.Connected, "Disconnect #1");
                        
@@ -2529,13 +2463,31 @@ namespace MonoTests.System.Net.Sockets
                public void IOControl ()
                {
                }
+
+               [Test]
+               public void TestDefaultsDualMode ()
+               {
+                       using (var socket = new Socket (AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp)){
+                               Assert.IsTrue (socket.DualMode, "In Mono, DualMode must be true when constructing InterNetworkV6 sockets");
+                       }
+
+                       using (var socket = new Socket (SocketType.Stream, ProtocolType.Tcp)){
+                               Assert.AreEqual (AddressFamily.InterNetworkV6, socket.AddressFamily, "When creating sockets of type stream/tcp, the address family should be InterNetworkV6");
+                               Assert.IsTrue (socket.DualMode, "In Mono, DualMode must be true when constructing InterNetworkV6 sockets");
+
+                               socket.DualMode = false;
+
+                               Assert.IsFalse (socket.DualMode, "Setting of DualSocket should turn DualSockets off");
+                       }
+                       
+               }
                
                [Test]
                public void ReceiveGeneric ()
                {
                        int i;
 
-                       IPEndPoint endpoint = new IPEndPoint(IPAddress.Loopback, 1258);
+                       IPEndPoint endpoint = new IPEndPoint(IPAddress.Loopback, NetworkHelpers.FindFreePort ());
 
                        Socket listensock = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                        listensock.Bind (endpoint);
@@ -2592,7 +2544,7 @@ namespace MonoTests.System.Net.Sockets
                {
                        int i;
 
-                       IPEndPoint endpoint = new IPEndPoint(IPAddress.Loopback, 1259);
+                       IPEndPoint endpoint = new IPEndPoint(IPAddress.Loopback, NetworkHelpers.FindFreePort ());
 
                        Socket listensock = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                        listensock.Bind (endpoint);
@@ -2657,13 +2609,10 @@ namespace MonoTests.System.Net.Sockets
                                Assert.Fail ("ListenNotBound #1");
                        } catch (SocketException ex) {
                                Assert.AreEqual (10022, ex.ErrorCode, "ListenNotBound #2");
-                       } catch {
-                               Assert.Fail ("ListenNotBound #3");
                        } finally {
                                sock.Close ();
                        }
                }
-#endif
 
                static Socket CWRSocket;
                static bool CWRReceiving = true;
@@ -2689,7 +2638,7 @@ namespace MonoTests.System.Net.Sockets
                                                SocketType.Dgram,
                                                ProtocolType.Udp);
                        CWRSocket.Bind (new IPEndPoint (IPAddress.Loopback,
-                                                       1256));
+                                                       NetworkHelpers.FindFreePort ()));
                        
                        Thread recv_thread = new Thread (new ThreadStart (CWRReceiveThread));
                        CWRReady.Reset ();
@@ -2707,7 +2656,7 @@ namespace MonoTests.System.Net.Sockets
                static bool RRCLastRead = false;
                static ManualResetEvent RRCReady = new ManualResetEvent (false);
                
-               private static void RRCClientThread ()
+               private static void RRCClientThread (int port)
                {
                        byte[] bytes = new byte[8];
                        int readbyte;
@@ -2716,7 +2665,7 @@ namespace MonoTests.System.Net.Sockets
                                                  SocketType.Stream,
                                                  ProtocolType.Tcp);
                        sock.Connect (new IPEndPoint (IPAddress.Loopback,
-                                                     1257));
+                                                     port));
                        
                        NetworkStream stream = new NetworkStream (sock);
 
@@ -2891,7 +2840,6 @@ namespace MonoTests.System.Net.Sockets
                        }
                }
 
-#if NET_2_0
                [Test] // Receive (Byte [], Int32, Int32, SocketFlags, out SocketError)
                public void Receive5_Buffer_Null ()
                {
@@ -3051,7 +2999,6 @@ namespace MonoTests.System.Net.Sockets
                                s.Close ();
                        }
                }
-#endif
 
                [Test] // ReceiveFrom (Byte [], ref EndPoint)
                public void ReceiveFrom1_Buffer_Null ()
@@ -3059,7 +3006,7 @@ namespace MonoTests.System.Net.Sockets
                        Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
                                ProtocolType.Tcp);
 
-                       EndPoint remoteEP = new IPEndPoint (IPAddress.Loopback, 8001);
+                       EndPoint remoteEP = new IPEndPoint (IPAddress.Loopback, NetworkHelpers.FindFreePort ());
                        try {
                                s.ReceiveFrom ((Byte []) null, ref remoteEP);
                                Assert.Fail ("#1");
@@ -3101,7 +3048,7 @@ namespace MonoTests.System.Net.Sockets
                                ProtocolType.Tcp);
                        s.Close ();
 
-                       EndPoint remoteEP = new IPEndPoint (IPAddress.Loopback, 8001);
+                       EndPoint remoteEP = new IPEndPoint (IPAddress.Loopback, NetworkHelpers.FindFreePort ());
                        try {
                                s.ReceiveFrom ((Byte []) null, ref remoteEP);
                                Assert.Fail ("#1");
@@ -3120,7 +3067,7 @@ namespace MonoTests.System.Net.Sockets
                        Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
                                ProtocolType.Tcp);
 
-                       EndPoint remoteEP = new IPEndPoint (IPAddress.Loopback, 8001);
+                       EndPoint remoteEP = new IPEndPoint (IPAddress.Loopback, NetworkHelpers.FindFreePort ());
                        try {
                                s.ReceiveFrom ((Byte []) null, (SocketFlags) 666, ref remoteEP);
                                Assert.Fail ("#1");
@@ -3162,7 +3109,7 @@ namespace MonoTests.System.Net.Sockets
                                ProtocolType.Tcp);
                        s.Close ();
 
-                       EndPoint remoteEP = new IPEndPoint (IPAddress.Loopback, 8001);
+                       EndPoint remoteEP = new IPEndPoint (IPAddress.Loopback, NetworkHelpers.FindFreePort ());
                        try {
                                s.ReceiveFrom ((Byte []) null, (SocketFlags) 666, ref remoteEP);
                                Assert.Fail ("#1");
@@ -3181,9 +3128,9 @@ namespace MonoTests.System.Net.Sockets
                        Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
                                ProtocolType.Tcp);
 
-                       EndPoint remoteEP = new IPEndPoint (IPAddress.Loopback, 8001);
+                       EndPoint remoteEP = new IPEndPoint (IPAddress.Loopback, NetworkHelpers.FindFreePort ());
                        try {
-                               s.ReceiveFrom ((Byte []) null, -1, (SocketFlags) 666,
+                               s.ReceiveFrom ((Byte []) null, 0, (SocketFlags) 666,
                                        ref remoteEP);
                                Assert.Fail ("#1");
                        } catch (ArgumentNullException ex) {
@@ -3205,7 +3152,7 @@ namespace MonoTests.System.Net.Sockets
                        byte [] buffer = new byte [5];
                        EndPoint remoteEP = null;
                        try {
-                               s.ReceiveFrom (buffer, -1, (SocketFlags) 666, ref remoteEP);
+                               s.ReceiveFrom (buffer, buffer.Length, (SocketFlags) 666, ref remoteEP);
                                Assert.Fail ("#1");
                        } catch (ArgumentNullException ex) {
                                Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
@@ -3222,7 +3169,7 @@ namespace MonoTests.System.Net.Sockets
                {
                        Socket s;
                        byte [] buffer = new byte [5];
-                       EndPoint remoteEP = new IPEndPoint (IPAddress.Loopback, 8001);
+                       EndPoint remoteEP = new IPEndPoint (IPAddress.Loopback, NetworkHelpers.FindFreePort ());
 
                        // size negative
                        s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
@@ -3265,7 +3212,7 @@ namespace MonoTests.System.Net.Sockets
                                ProtocolType.Tcp);
                        s.Close ();
 
-                       EndPoint remoteEP = new IPEndPoint (IPAddress.Loopback, 8001);
+                       EndPoint remoteEP = new IPEndPoint (IPAddress.Loopback, NetworkHelpers.FindFreePort ());
                        try {
                                s.ReceiveFrom ((Byte []) null, -1, (SocketFlags) 666,
                                        ref remoteEP);
@@ -3284,7 +3231,7 @@ namespace MonoTests.System.Net.Sockets
                {
                        Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
                                ProtocolType.Tcp);
-                       EndPoint remoteEP = new IPEndPoint (IPAddress.Loopback, 8001);
+                       EndPoint remoteEP = new IPEndPoint (IPAddress.Loopback, NetworkHelpers.FindFreePort ());
 
                        try {
                                s.ReceiveFrom ((Byte []) null, -1, -1, (SocketFlags) 666,
@@ -3303,7 +3250,7 @@ namespace MonoTests.System.Net.Sockets
                {
                        Socket s;
                        byte [] buffer = new byte [5];
-                       EndPoint remoteEP = new IPEndPoint (IPAddress.Loopback, 8001);
+                       EndPoint remoteEP = new IPEndPoint (IPAddress.Loopback, NetworkHelpers.FindFreePort ());
 
                        // offset negative
                        s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
@@ -3349,7 +3296,7 @@ namespace MonoTests.System.Net.Sockets
                        EndPoint remoteEP = null;
 
                        try {
-                               s.ReceiveFrom (buffer, -1, -1, (SocketFlags) 666, ref remoteEP);
+                               s.ReceiveFrom (buffer, 0, buffer.Length, (SocketFlags) 666, ref remoteEP);
                                Assert.Fail ("#1");
                        } catch (ArgumentNullException ex) {
                                Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
@@ -3366,7 +3313,7 @@ namespace MonoTests.System.Net.Sockets
                {
                        Socket s;
                        byte [] buffer = new byte [5];
-                       EndPoint remoteEP = new IPEndPoint (IPAddress.Loopback, 8001);
+                       EndPoint remoteEP = new IPEndPoint (IPAddress.Loopback, NetworkHelpers.FindFreePort ());
 
                        // size negative
                        s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
@@ -3427,7 +3374,7 @@ namespace MonoTests.System.Net.Sockets
                        s.Close ();
 
                        byte [] buffer = new byte [5];
-                       EndPoint remoteEP = new IPEndPoint (IPAddress.Loopback, 8001);
+                       EndPoint remoteEP = new IPEndPoint (IPAddress.Loopback, NetworkHelpers.FindFreePort ());
                        try {
                                s.ReceiveFrom (buffer, -1, -1, (SocketFlags) 666,
                                        ref remoteEP);
@@ -3444,14 +3391,15 @@ namespace MonoTests.System.Net.Sockets
                [Test]
                public void ReceiveRemoteClosed ()
                {
+                       var port = NetworkHelpers.FindFreePort ();
                        Socket sock = new Socket (AddressFamily.InterNetwork,
                                                  SocketType.Stream,
                                                  ProtocolType.Tcp);
-                       sock.Bind (new IPEndPoint (IPAddress.Loopback, 1257));
+                       sock.Bind (new IPEndPoint (IPAddress.Loopback, port));
                        sock.Listen (1);
                        
                        RRCReady.Reset ();
-                       Thread client_thread = new Thread (new ThreadStart (RRCClientThread));
+                       Thread client_thread = new Thread (() => RRCClientThread (port));
                        client_thread.Start ();
                        
                        Socket client = sock.Accept ();
@@ -3470,32 +3418,79 @@ namespace MonoTests.System.Net.Sockets
                [Test]
                public void UdpDoubleBind ()
                {
-                       Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
-                       s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, 1);
-                       
-                       s.Bind (new IPEndPoint (IPAddress.Any, 12345));
-                       
-                       Socket ss = new Socket (AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
-                       ss.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, 1);
-                       
-                       ss.Bind (new IPEndPoint (IPAddress.Any, 12345));
+                       using (Socket s = new Socket (AddressFamily.InterNetwork,
+                                               SocketType.Dgram, ProtocolType.Udp))
+                       using (Socket ss = new Socket (AddressFamily.InterNetwork,
+                                               SocketType.Dgram, ProtocolType.Udp)) {
+                               var supportsReuseAddress = true;
+                               try {
+                                       s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
+                               } catch (SocketException e) {
+                                       // Exception is thrown when ReuseAddress is not supported
+                                       Assert.AreEqual ((int) SocketError.OperationNotSupported, e.NativeErrorCode,
+                                                       "Expected SocketError.OperationNotSupported");
+                                       supportsReuseAddress = false;
+                               }
 
-                       // If we make it this far, we succeeded.
-                       
-                       ss.Close ();
-                       s.Close ();
+                               var ep = new IPEndPoint (IPAddress.Any, NetworkHelpers.FindFreePort ());
+                               s.Bind (ep);
+
+                               if (supportsReuseAddress)
+                                       ss.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
+
+                               try {
+                                       ss.Bind (new IPEndPoint (IPAddress.Any, ep.Port));
+                                       if (!supportsReuseAddress)
+                                               Assert.Fail ("Reusing address is not supported, exception was expected on second bind.");
+                               } catch (SocketException e) {
+                               }
+                       }
                }
-               
-#if NET_2_0
+
+               // Test case for bug #31557
+               [Test]
+               public void TcpDoubleBind ()
+               {
+                       using (Socket s = new Socket (AddressFamily.InterNetwork,
+                                               SocketType.Stream, ProtocolType.Tcp))
+                       using (Socket ss = new Socket (AddressFamily.InterNetwork,
+                                               SocketType.Stream, ProtocolType.Tcp)) {
+                               var supportsReuseAddress = true;
+                               try {
+                                       s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
+                               } catch (SocketException e) {
+                                       // Exception is thrown when ReuseAddress is not supported
+                                       Assert.AreEqual ((int) SocketError.OperationNotSupported, e.NativeErrorCode,
+                                                       "Expected SocketError.OperationNotSupported");
+                                       supportsReuseAddress = false;
+                               }
+
+                               var ep = new IPEndPoint (IPAddress.Any, NetworkHelpers.FindFreePort ());
+                               s.Bind (ep);
+                               s.Listen(1);
+
+                               if (supportsReuseAddress)
+                                       ss.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
+
+                               try {
+                                       ss.Bind (new IPEndPoint (IPAddress.Any, ep.Port));
+                                       ss.Listen(1);
+                                       if (!supportsReuseAddress)
+                                               Assert.Fail ("Reusing address is not supported, exception was expected on second bind.");
+                               } catch (SocketException e) {
+                               }
+                       }
+               }
+
                [Test]
                [Category ("NotOnMac")]
                 public void ConnectedProperty ()
                 {
-                       TcpListener listener = new TcpListener (IPAddress.Loopback, 23456);
+                       TcpListener listener = new TcpListener (IPAddress.Loopback, NetworkHelpers.FindFreePort ());
                        listener.Start();
 
                        Socket client = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
-                       client.Connect (IPAddress.Loopback, 23456);
+                       client.Connect (IPAddress.Loopback, ((IPEndPoint)listener.LocalEndpoint).Port);
                        Socket server = listener.AcceptSocket ();
 
                        try {
@@ -3513,7 +3508,6 @@ namespace MonoTests.System.Net.Sockets
                                server.Close ();
                        }
                }
-#endif
 
                [Test] // GetSocketOption (SocketOptionLevel, SocketOptionName)
                public void GetSocketOption1_Socket_Closed ()
@@ -3548,9 +3542,7 @@ namespace MonoTests.System.Net.Sockets
                                        Assert.IsNull (ex.InnerException, "#4");
                                        Assert.IsNotNull (ex.Message, "#5");
                                        Assert.AreEqual (10014, ex.NativeErrorCode, "#6");
-#if NET_2_0
                                        Assert.AreEqual (SocketError.Fault, ex.SocketErrorCode, "#7");
-#endif
                                }
                }
 
@@ -3615,9 +3607,7 @@ namespace MonoTests.System.Net.Sockets
                                        Assert.IsNull (ex.InnerException, "#4");
                                        Assert.IsNotNull (ex.Message, "#5");
                                        Assert.AreEqual (10014, ex.NativeErrorCode, "#6");
-#if NET_2_0
                                        Assert.AreEqual (SocketError.Fault, ex.SocketErrorCode, "#7");
-#endif
                                }
                        }
                }
@@ -3638,9 +3628,7 @@ namespace MonoTests.System.Net.Sockets
                                        Assert.IsNull (ex.InnerException, "#4");
                                        Assert.IsNotNull (ex.Message, "#5");
                                        Assert.AreEqual (10014, ex.NativeErrorCode, "#6");
-#if NET_2_0
                                        Assert.AreEqual (SocketError.Fault, ex.SocketErrorCode, "#7");
-#endif
                                }
                        }
                }
@@ -3704,7 +3692,7 @@ namespace MonoTests.System.Net.Sockets
                        IPAddress mcast_addr = IPAddress.Parse ("239.255.255.250");
 
                        using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)) {
-                               s.Bind (new IPEndPoint (IPAddress.Any, 1901));
+                               s.Bind (new IPEndPoint (IPAddress.Any, NetworkHelpers.FindFreePort ()));
                                try {
                                        s.SetSocketOption (SocketOptionLevel.IP, SocketOptionName.AddMembership,
                                                new IPv6MulticastOption (mcast_addr));
@@ -3713,14 +3701,9 @@ namespace MonoTests.System.Net.Sockets
                                        Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
                                        Assert.IsNull (ex.InnerException, "#3");
                                        Assert.IsNotNull (ex.Message, "#4");
-#if NET_2_0
                                        // The specified value is not a valid 'MulticastOption'
                                        Assert.IsTrue (ex.Message.IndexOf ("'MulticastOption'") != -1, "#5:" + ex.Message);
                                        Assert.AreEqual ("optionValue", ex.ParamName, "#6");
-#else
-                                       Assert.AreEqual ("optionValue", ex.Message, "#5");
-                                       Assert.IsNull (ex.ParamName, "#6");
-#endif
                                }
                        }
                }
@@ -3731,7 +3714,7 @@ namespace MonoTests.System.Net.Sockets
                        IPAddress mcast_addr = IPAddress.Parse ("239.255.255.250");
 
                        using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)) {
-                               s.Bind (new IPEndPoint (IPAddress.Any, 1901));
+                               s.Bind (new IPEndPoint (IPAddress.Any, NetworkHelpers.FindFreePort ()));
                                s.SetSocketOption (SocketOptionLevel.IP, SocketOptionName.AddMembership,
                                        new MulticastOption (mcast_addr));
                        }
@@ -3755,9 +3738,7 @@ namespace MonoTests.System.Net.Sockets
                                Assert.IsNull (ex.InnerException, "#4");
                                Assert.IsNotNull (ex.Message, "#5");
                                Assert.AreEqual (10022, ex.NativeErrorCode, "#6");
-#if NET_2_0
                                Assert.AreEqual (SocketError.InvalidArgument, ex.SocketErrorCode, "#7");
-#endif
                        } finally {
                                s.Close ();
                        }
@@ -3766,17 +3747,13 @@ namespace MonoTests.System.Net.Sockets
                [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
                public void SetSocketOption3_AddMembershipIPv6_IPv6MulticastOption ()
                {
-#if NET_2_0
                        if (!Socket.OSSupportsIPv6)
-#else
-                       if (!Socket.SupportsIPv6)
-#endif
                                Assert.Ignore ("IPv6 not enabled.");
 
                        IPAddress mcast_addr = IPAddress.Parse ("ff02::1");
 
                        using (Socket s = new Socket (AddressFamily.InterNetworkV6, SocketType.Dgram, ProtocolType.Udp)) {
-                               s.Bind (new IPEndPoint (IPAddress.IPv6Any, 1902));
+                               s.Bind (new IPEndPoint (IPAddress.IPv6Any, NetworkHelpers.FindFreePort ()));
                                s.SetSocketOption (SocketOptionLevel.IPv6, SocketOptionName.AddMembership,
                                        new IPv6MulticastOption (mcast_addr));
                        }
@@ -3785,17 +3762,13 @@ namespace MonoTests.System.Net.Sockets
                [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
                public void SetSocketOption3_AddMembershipIPv6_MulticastOption ()
                {
-#if NET_2_0
                        if (!Socket.OSSupportsIPv6)
-#else
-                       if (!Socket.SupportsIPv6)
-#endif
                                Assert.Ignore ("IPv6 not enabled.");
 
                        IPAddress mcast_addr = IPAddress.Parse ("ff02::1");
 
                        using (Socket s = new Socket (AddressFamily.InterNetworkV6, SocketType.Dgram, ProtocolType.Udp)) {
-                               s.Bind (new IPEndPoint (IPAddress.IPv6Any, 1902));
+                               s.Bind (new IPEndPoint (IPAddress.IPv6Any, NetworkHelpers.FindFreePort ()));
                                try {
                                        s.SetSocketOption (SocketOptionLevel.IPv6, SocketOptionName.AddMembership,
                                                new MulticastOption (mcast_addr));
@@ -3804,14 +3777,9 @@ namespace MonoTests.System.Net.Sockets
                                        Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
                                        Assert.IsNull (ex.InnerException, "#3");
                                        Assert.IsNotNull (ex.Message, "#4");
-#if NET_2_0
                                        // The specified value is not a valid 'IPv6MulticastOption'
                                        Assert.IsTrue (ex.Message.IndexOf ("'IPv6MulticastOption'") != -1, "#5:" + ex.Message);
                                        Assert.AreEqual ("optionValue", ex.ParamName, "#6");
-#else
-                                       Assert.AreEqual ("optionValue", ex.Message, "#5");
-                                       Assert.IsNull (ex.ParamName, "#6");
-#endif
                                }
                        }
                }
@@ -3834,9 +3802,7 @@ namespace MonoTests.System.Net.Sockets
                                Assert.IsNull (ex.InnerException, "#4");
                                Assert.IsNotNull (ex.Message, "#5");
                                Assert.AreEqual (10022, ex.NativeErrorCode, "#6");
-#if NET_2_0
                                Assert.AreEqual (SocketError.InvalidArgument, ex.SocketErrorCode, "#7");
-#endif
                        } finally {
                                s.Close ();
                        }
@@ -3854,13 +3820,8 @@ namespace MonoTests.System.Net.Sockets
                                        // The specified value is not valid
                                        Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
                                        Assert.IsNull (ex.InnerException, "#3");
-#if NET_2_0
                                        Assert.IsNotNull (ex.Message, "#4");
                                        Assert.AreEqual ("optionValue", ex.ParamName, "#5");
-#else
-                                       Assert.AreEqual ("optionValue", ex.Message, "#4");
-                                       Assert.IsNull (ex.ParamName, "#5");
-#endif
                                }
                        }
                }
@@ -3877,13 +3838,8 @@ namespace MonoTests.System.Net.Sockets
                                        // The specified value is not valid
                                        Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
                                        Assert.IsNull (ex.InnerException, "#3");
-#if NET_2_0
                                        Assert.IsNotNull (ex.Message, "#4");
                                        Assert.AreEqual ("optionValue", ex.ParamName, "#5");
-#else
-                                       Assert.AreEqual ("optionValue", ex.Message, "#4");
-                                       Assert.IsNull (ex.ParamName, "#5");
-#endif
                                }
                        }
                }
@@ -3899,14 +3855,9 @@ namespace MonoTests.System.Net.Sockets
                                } catch (ArgumentException ex) {
                                        Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
                                        Assert.IsNull (ex.InnerException, "#3");
-#if NET_2_0
                                        // The specified value is not valid
                                        Assert.IsNotNull (ex.Message, "#4");
                                        Assert.AreEqual ("optionValue", ex.ParamName, "#5");
-#else
-                                       Assert.AreEqual ("optionValue", ex.Message, "#4");
-                                       Assert.IsNull (ex.ParamName, "#5");
-#endif
                                }
                        }
                }
@@ -3922,14 +3873,9 @@ namespace MonoTests.System.Net.Sockets
                                } catch (ArgumentException ex) {
                                        Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
                                        Assert.IsNull (ex.InnerException, "#3");
-#if NET_2_0
                                        // The specified value is not valid
                                        Assert.IsNotNull (ex.Message, "#4");
                                        Assert.AreEqual ("optionValue", ex.ParamName, "#5");
-#else
-                                       Assert.AreEqual ("optionValue", ex.Message, "#4");
-                                       Assert.IsNull (ex.ParamName, "#5");
-#endif
                                }
                        }
                }
@@ -3945,14 +3891,9 @@ namespace MonoTests.System.Net.Sockets
                                } catch (ArgumentException ex) {
                                        Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
                                        Assert.IsNull (ex.InnerException, "#3");
-#if NET_2_0
                                        // The specified value is not valid
                                        Assert.IsNotNull (ex.Message, "#4");
                                        Assert.AreEqual ("optionValue", ex.ParamName, "#5");
-#else
-                                       Assert.AreEqual ("optionValue", ex.Message, "#4");
-                                       Assert.IsNull (ex.ParamName, "#5");
-#endif
                                }
                        }
                }
@@ -3978,7 +3919,7 @@ namespace MonoTests.System.Net.Sockets
                        IPAddress mcast_addr = IPAddress.Parse ("239.255.255.250");
 
                        using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)) {
-                               s.Bind (new IPEndPoint (IPAddress.Any, 1901));
+                               s.Bind (new IPEndPoint (IPAddress.Any, NetworkHelpers.FindFreePort ()));
                                s.SetSocketOption (SocketOptionLevel.IP, SocketOptionName.AddMembership,
                                        new MulticastOption (mcast_addr));
                                try {
@@ -3989,14 +3930,9 @@ namespace MonoTests.System.Net.Sockets
                                        Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
                                        Assert.IsNull (ex.InnerException, "#3");
                                        Assert.IsNotNull (ex.Message, "#4");
-#if NET_2_0
                                        // The specified value is not a valid 'MulticastOption'
                                        Assert.IsTrue (ex.Message.IndexOf ("'MulticastOption'") != -1, "#5:" + ex.Message);
                                        Assert.AreEqual ("optionValue", ex.ParamName, "#6");
-#else
-                                       Assert.AreEqual ("optionValue", ex.Message, "#5");
-                                       Assert.IsNull (ex.ParamName, "#6");
-#endif
                                }
                        }
                }
@@ -4009,7 +3945,7 @@ namespace MonoTests.System.Net.Sockets
                        using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)) {
                                MulticastOption option = new MulticastOption (mcast_addr);
 
-                               s.Bind (new IPEndPoint (IPAddress.Any, 1901));
+                               s.Bind (new IPEndPoint (IPAddress.Any, NetworkHelpers.FindFreePort ()));
                                s.SetSocketOption (SocketOptionLevel.IP, SocketOptionName.AddMembership,
                                        option);
                                s.SetSocketOption (SocketOptionLevel.IP, SocketOptionName.DropMembership,
@@ -4035,9 +3971,7 @@ namespace MonoTests.System.Net.Sockets
                                Assert.IsNull (ex.InnerException, "#4");
                                Assert.IsNotNull (ex.Message, "#5");
                                Assert.AreEqual (10022, ex.NativeErrorCode, "#6");
-#if NET_2_0
                                Assert.AreEqual (SocketError.InvalidArgument, ex.SocketErrorCode, "#7");
-#endif
                        } finally {
                                s.Close ();
                        }
@@ -4046,11 +3980,7 @@ namespace MonoTests.System.Net.Sockets
                [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
                public void SetSocketOption3_DropMembershipIPv6_IPv6MulticastOption ()
                {
-#if NET_2_0
                        if (!Socket.OSSupportsIPv6)
-#else
-                       if (!Socket.SupportsIPv6)
-#endif
                                Assert.Ignore ("IPv6 not enabled.");
 
                        using (Socket s = new Socket (AddressFamily.InterNetworkV6, SocketType.Dgram, ProtocolType.Udp)) {
@@ -4068,17 +3998,13 @@ namespace MonoTests.System.Net.Sockets
                [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
                public void SetSocketOption3_DropMembershipIPv6_MulticastOption ()
                {
-#if NET_2_0
                        if (!Socket.OSSupportsIPv6)
-#else
-                       if (!Socket.SupportsIPv6)
-#endif
                                Assert.Ignore ("IPv6 not enabled.");
 
                        IPAddress mcast_addr = IPAddress.Parse ("ff02::1");
 
                        using (Socket s = new Socket (AddressFamily.InterNetworkV6, SocketType.Dgram, ProtocolType.Udp)) {
-                               s.Bind (new IPEndPoint (IPAddress.IPv6Any, 1902));
+                               s.Bind (new IPEndPoint (IPAddress.IPv6Any, NetworkHelpers.FindFreePort ()));
                                s.SetSocketOption (SocketOptionLevel.IPv6, SocketOptionName.AddMembership,
                                        new IPv6MulticastOption (mcast_addr));
                                try {
@@ -4089,14 +4015,9 @@ namespace MonoTests.System.Net.Sockets
                                        Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
                                        Assert.IsNull (ex.InnerException, "#3");
                                        Assert.IsNotNull (ex.Message, "#4");
-#if NET_2_0
                                        // The specified value is not a valid 'IPv6MulticastOption'
                                        Assert.IsTrue (ex.Message.IndexOf ("'IPv6MulticastOption'") != -1, "#5:" + ex.Message);
                                        Assert.AreEqual ("optionValue", ex.ParamName, "#6");
-#else
-                                       Assert.AreEqual ("optionValue", ex.Message, "#5");
-                                       Assert.IsNull (ex.ParamName, "#6");
-#endif
                                }
                        }
                }
@@ -4119,9 +4040,7 @@ namespace MonoTests.System.Net.Sockets
                                Assert.IsNull (ex.InnerException, "#4");
                                Assert.IsNotNull (ex.Message, "#5");
                                Assert.AreEqual (10022, ex.NativeErrorCode, "#6");
-#if NET_2_0
                                Assert.AreEqual (SocketError.InvalidArgument, ex.SocketErrorCode, "#7");
-#endif
                        } finally {
                                s.Close ();
                        }
@@ -4214,7 +4133,7 @@ namespace MonoTests.System.Net.Sockets
                public void Shutdown_NoConnect ()
                {
                        Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
-                       s.Bind (new IPEndPoint (IPAddress.Loopback, 0));
+                       s.Bind (new IPEndPoint (IPAddress.Loopback, NetworkHelpers.FindFreePort ()));
                        s.Listen (1);
                        try {
                                s.Shutdown (SocketShutdown.Both);
@@ -4309,7 +4228,22 @@ namespace MonoTests.System.Net.Sockets
                [Test]
                public void SendAsyncFile ()
                {
-                       Socket serverSocket = StartSocketServer ();
+                       Socket serverSocket = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
+
+                       serverSocket.Bind (new IPEndPoint (IPAddress.Loopback, 0));
+                       serverSocket.Listen (1);
+
+                       var mReceived = new ManualResetEvent (false);
+
+                       serverSocket.BeginAccept (AsyncCall => {
+                               byte[] bytes = new byte [1024];
+
+                               Socket listener = (Socket)AsyncCall.AsyncState;
+                               Socket client = listener.EndAccept (AsyncCall);
+                               client.Receive (bytes, bytes.Length, 0);
+                               client.Close ();
+                               mReceived.Set ();
+                       }, serverSocket);
                        
                        Socket clientSocket = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                        clientSocket.Connect (serverSocket.LocalEndPoint);
@@ -4327,18 +4261,19 @@ namespace MonoTests.System.Net.Sockets
                                        sw.Write (buffer);
                                }
 
-                               var m = new ManualResetEvent (false);
+                               var mSent = new ManualResetEvent (false);
 
                                // Async Send File to server
                                clientSocket.BeginSendFile(temp, (ar) => {
                                        Socket client = (Socket) ar.AsyncState;
                                        client.EndSendFile (ar);
-                                       m.Set ();
+                                       mSent.Set ();
                                }, clientSocket);
 
-                               if (!m.WaitOne (1500))
+                               if (!mSent.WaitOne (1500))
+                                       throw new TimeoutException ();
+                               if (!mReceived.WaitOne (1500))
                                        throw new TimeoutException ();
-                               m.Reset ();
                        } finally {
                                if (File.Exists (temp))
                                        File.Delete (temp);
@@ -4348,28 +4283,67 @@ namespace MonoTests.System.Net.Sockets
                        }
                }
                
-               Socket StartSocketServer ()
-               {
-
-                       Socket listenSocket = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
-                       
-                       listenSocket.Bind (new IPEndPoint (IPAddress.Loopback, 8001));
-                       listenSocket.Listen (1);
-
-                       listenSocket.BeginAccept (new AsyncCallback (ReceiveCallback), listenSocket);
-                       
-                       return listenSocket;
+               [Test]
+               public void ConnectToIPV4EndPointUsingDualModelSocket () {
+                       using (var server = new Socket (SocketType.Stream, ProtocolType.Tcp))
+                       using (var client = new Socket (SocketType.Stream, ProtocolType.Tcp)) {
+                               var host = new IPEndPoint (IPAddress.Loopback, NetworkHelpers.FindFreePort ());
+                                       
+                               server.Bind (host);
+                               server.Listen (0);
+                               
+                               var ep = server.LocalEndPoint as IPEndPoint;
+                               
+                               client.Connect (ep);
+                               client.Disconnect (true);
+                               
+                               client.Connect (IPAddress.Loopback, ep.Port);
+                               client.Disconnect (true);
+                               
+                               client.Connect (new [] {IPAddress.Loopback}, ep.Port);
+                               client.Disconnect (true);
+                       }
                }
-
-               public static void ReceiveCallback (IAsyncResult AsyncCall)
-               {
-                       byte[] bytes = new byte [1024];
-
-                       Socket listener = (Socket)AsyncCall.AsyncState;
-                       Socket client = listener.EndAccept (AsyncCall);
-                       client.Receive (bytes, bytes.Length, 0);
-                       client.Close ();
+               
+               [Test]
+               public void BeginConnectToIPV4EndPointUsingDualModelSocket () {
+                       using (var server = new Socket (SocketType.Stream, ProtocolType.Tcp))
+                       using (var client = new Socket (SocketType.Stream, ProtocolType.Tcp)) {
+                               var host = new IPEndPoint (IPAddress.Loopback, NetworkHelpers.FindFreePort ());
+                                       
+                               server.Bind (host);
+                               server.Listen (0);
+                               
+                               var ep = server.LocalEndPoint as IPEndPoint;
+                               
+                               BCCalledBack.Reset ();
+                               var ar1 = client.BeginConnect (ep, BCCallback, client);
+                               Assert.IsTrue (BCCalledBack.WaitOne (10000), "#1");
+                               client.Disconnect (true);
+                               
+                               BCCalledBack.Reset ();
+                               var ar2 = client.BeginConnect (IPAddress.Loopback, ep.Port, BCCallback, client);
+                               Assert.IsTrue (BCCalledBack.WaitOne (10000), "#2");
+                               client.Disconnect (true);
+                               
+                               BCCalledBack.Reset ();
+                               var ar3 = client.BeginConnect (new [] {IPAddress.Loopback}, ep.Port, BCCallback, client);
+                               Assert.IsTrue (BCCalledBack.WaitOne (10000), "#2");
+                               client.Disconnect (true);
+                       }
+               }
+
+               [Test]
+               public void UdpMulticasTimeToLive ()
+               {
+                       /* see https://bugzilla.xamarin.com/show_bug.cgi?id=36941 */
+
+                       using (Socket socket = new Socket (AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)) {
+                               IPEndPoint end_point = new IPEndPoint (IPAddress.Any, NetworkHelpers.FindFreePort ());
+                               socket.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, 1);
+                               socket.Bind (end_point);
+                               socket.SetSocketOption (SocketOptionLevel.IP, SocketOptionName.MulticastTimeToLive, 19);
+                       }
                }
        }
 }