[socket] Add EPROTOTYPE error case (#4391)
[mono.git] / mcs / class / System / Test / System.Net.Sockets / SocketTest.cs
index a53a5d5624ab010ece48667b8bfe72719d856110..bce47bcbf0dfc43890e39c8e81e064005e49a0ec 100755 (executable)
@@ -15,6 +15,7 @@ using System.Collections;
 using System.Threading;
 using System.Reflection;
 using System.Text.RegularExpressions;
+using System.Threading.Tasks;
 using System.Net;
 using System.Net.Sockets;
 using NUnit.Framework;
@@ -34,6 +35,9 @@ namespace MonoTests.System.Net.Sockets
                public const int BogusPort = 23483;
 
                [Test]
+#if FEATURE_NO_BSD_SOCKETS
+               [ExpectedException (typeof (PlatformNotSupportedException))]
+#endif
                public void ConnectIPAddressAny ()
                {
                        IPEndPoint ep = new IPEndPoint (IPAddress.Any, NetworkHelpers.FindFreePort ());
@@ -84,6 +88,9 @@ namespace MonoTests.System.Net.Sockets
 
                [Test]
                [Category ("InetAccess")]
+#if FEATURE_NO_BSD_SOCKETS
+               [ExpectedException (typeof (PlatformNotSupportedException))]
+#endif
                public void BogusEndConnect ()
                {
                        IPAddress ipOne = IPAddress.Parse (BogusAddress);
@@ -154,6 +161,9 @@ namespace MonoTests.System.Net.Sockets
                }
 
                [Test]
+#if FEATURE_NO_BSD_SOCKETS
+               [ExpectedException (typeof (PlatformNotSupportedException))]
+#endif
                public void AcceptBlockingStatus()
                {
                        bool block;
@@ -198,6 +208,9 @@ namespace MonoTests.System.Net.Sockets
                }
 
                [Test]
+#if FEATURE_NO_BSD_SOCKETS
+               [ExpectedException (typeof (PlatformNotSupportedException))]
+#endif
                public void ConnectFailAsync ()
                {
                        Socket sock = new Socket (AddressFamily.InterNetwork,
@@ -220,6 +233,9 @@ namespace MonoTests.System.Net.Sockets
                }
                
                [Test]
+#if FEATURE_NO_BSD_SOCKETS
+               [ExpectedException (typeof (PlatformNotSupportedException))]
+#endif
                public void SetSocketOptionBoolean ()
                {
                        IPEndPoint ep = new IPEndPoint (IPAddress.Loopback, NetworkHelpers.FindFreePort ());
@@ -231,6 +247,9 @@ namespace MonoTests.System.Net.Sockets
                        }
                }
                [Test]
+#if FEATURE_NO_BSD_SOCKETS
+               [ExpectedException (typeof (PlatformNotSupportedException))]
+#endif
                public void TestSelect1 ()
                {
                        Socket srv = CreateServer (NetworkHelpers.FindFreePort ());
@@ -368,7 +387,11 @@ namespace MonoTests.System.Net.Sockets
                }
 
                [Test]
+#if FEATURE_NO_BSD_SOCKETS
+               [ExpectedException (typeof (PlatformNotSupportedException))]
+#else
                [ExpectedException (typeof (ObjectDisposedException))]
+#endif
                public void Disposed19 ()
                {
                        Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
@@ -379,7 +402,11 @@ namespace MonoTests.System.Net.Sockets
                }
 
                [Test]
+#if FEATURE_NO_BSD_SOCKETS
+               [ExpectedException (typeof (PlatformNotSupportedException))]
+#else
                [ExpectedException (typeof (ObjectDisposedException))]
+#endif
                public void Disposed20 ()
                {
                        Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
@@ -390,7 +417,11 @@ namespace MonoTests.System.Net.Sockets
                }
 
                [Test]
+#if FEATURE_NO_BSD_SOCKETS
+               [ExpectedException (typeof (PlatformNotSupportedException))]
+#else
                [ExpectedException (typeof (ObjectDisposedException))]
+#endif
                public void Disposed21 ()
                {
                        Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
@@ -401,7 +432,11 @@ namespace MonoTests.System.Net.Sockets
                }
 
                [Test]
+#if FEATURE_NO_BSD_SOCKETS
+               [ExpectedException (typeof (PlatformNotSupportedException))]
+#else
                [ExpectedException (typeof (ObjectDisposedException))]
+#endif
                public void Disposed22 ()
                {
                        Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
@@ -422,6 +457,9 @@ namespace MonoTests.System.Net.Sockets
                }
 
                [Test]
+#if FEATURE_NO_BSD_SOCKETS
+               [ExpectedException (typeof (PlatformNotSupportedException))]
+#endif
                public void GetHashCodeTest ()
                {
                        Socket server = new Socket (AddressFamily.InterNetwork,
@@ -457,6 +495,7 @@ namespace MonoTests.System.Net.Sockets
                }
 
                [Test]
+               [Category ("RequiresBSDSockets")] // This verifies particular error codes, which we don't care about when nothing's working anyway.
                public void SocketErrorTest ()
                {
                        Socket sock = new Socket (AddressFamily.InterNetwork,
@@ -573,8 +612,6 @@ namespace MonoTests.System.Net.Sockets
                                sock.DontFragment = true;
                                Assert.Fail ("DontFragment #1");
                        } catch (NotSupportedException) {
-                       } catch {
-                               Assert.Fail ("DontFragment #2");
                        } finally {
                                sock.Close ();
                        }
@@ -592,8 +629,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 ();
                        }
@@ -623,8 +658,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 ();
                        }
@@ -704,6 +737,9 @@ namespace MonoTests.System.Net.Sockets
                }
                
                [Test]
+#if FEATURE_NO_BSD_SOCKETS
+               [ExpectedException (typeof (PlatformNotSupportedException))]
+#endif
                public void IsBoundTcp ()
                {
                        Socket sock = new Socket (AddressFamily.InterNetwork,
@@ -736,6 +772,9 @@ namespace MonoTests.System.Net.Sockets
                }
 
                [Test]
+#if FEATURE_NO_BSD_SOCKETS
+               [ExpectedException (typeof (PlatformNotSupportedException))]
+#endif
                public void IsBoundUdp ()
                {
                        Socket sock = new Socket (AddressFamily.InterNetwork,
@@ -793,8 +832,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 ();
                        }
@@ -812,8 +849,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 ();
                        }
@@ -997,7 +1032,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,
@@ -1007,11 +1041,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 ();
                        }
@@ -1024,8 +1056,6 @@ namespace MonoTests.System.Net.Sockets
                        } catch (SocketException ex) {
                                Assert.AreEqual (10022, ex.ErrorCode,
                                                 "TtlChangeOverflow #5");
-                       } catch {
-                               Assert.Fail ("TtlChangeOverflow #6");
                        } finally {
                                sock.Close ();
                        }
@@ -1139,8 +1169,6 @@ namespace MonoTests.System.Net.Sockets
                                sock.SendTimeout = -2;
                                Assert.Fail ("SendTimeoutChange #8");
                        } catch (ArgumentOutOfRangeException) {
-                       } catch {
-                               Assert.Fail ("SendTimeoutChange #9");
                        } finally {
                                sock.Close ();
                        }
@@ -1198,8 +1226,6 @@ namespace MonoTests.System.Net.Sockets
                                sock.ReceiveTimeout = -2;
                                Assert.Fail ("ReceiveTimeoutChange #8");
                        } catch (ArgumentOutOfRangeException) {
-                       } catch {
-                               Assert.Fail ("ReceiveTimeoutChange #9");
                        } finally {
                                sock.Close ();
                        }
@@ -1224,9 +1250,9 @@ namespace MonoTests.System.Net.Sockets
                        Socket sock = new Socket (AddressFamily.InterNetwork,
                                                  SocketType.Stream,
                                                  ProtocolType.Tcp);
-                       
+
                        Assert.AreEqual (false, sock.NoDelay, "NoDelayDefaultTcp");
-                       
+
                        sock.Close ();
                }
 
@@ -1257,8 +1283,6 @@ namespace MonoTests.System.Net.Sockets
                        } catch (SocketException ex) {
                                Assert.AreEqual (10042, ex.ErrorCode,
                                                 "NoDelayDefaultUdp #2");
-                       } catch {
-                               Assert.Fail ("NoDelayDefaultUdp #3");
                        } finally {
                                sock.Close ();
                        }
@@ -1277,8 +1301,6 @@ namespace MonoTests.System.Net.Sockets
                        } catch (SocketException ex) {
                                Assert.AreEqual (10042, ex.ErrorCode,
                                                 "NoDelayChangeUdp #2");
-                       } catch {
-                               Assert.Fail ("NoDelayChangeUdp #3");
                        } finally {
                                sock.Close ();
                        }
@@ -1325,7 +1347,11 @@ namespace MonoTests.System.Net.Sockets
                }
                
                [Test]
+#if FEATURE_NO_BSD_SOCKETS
+               [ExpectedException (typeof (PlatformNotSupportedException))]
+#else
                [ExpectedException (typeof(InvalidOperationException))]
+#endif
                public void BeginAcceptNotListening ()
                {
                        Socket sock = new Socket (AddressFamily.InterNetwork,
@@ -1340,6 +1366,9 @@ namespace MonoTests.System.Net.Sockets
                }
 
                [Test]
+#if FEATURE_NO_BSD_SOCKETS
+               [ExpectedException (typeof (PlatformNotSupportedException))]
+#endif
                public void BeginAccept ()
                {
                        Socket sock = new Socket (AddressFamily.InterNetwork,
@@ -1407,6 +1436,9 @@ namespace MonoTests.System.Net.Sockets
                }
 
                [Test]
+#if FEATURE_NO_BSD_SOCKETS
+               [ExpectedException (typeof (PlatformNotSupportedException))]
+#endif
                public void BeginAcceptData ()
                {
                        Socket sock = new Socket (AddressFamily.InterNetwork,
@@ -1471,6 +1503,9 @@ namespace MonoTests.System.Net.Sockets
                }
 
                [Test]
+#if FEATURE_NO_BSD_SOCKETS
+               [ExpectedException (typeof (PlatformNotSupportedException))]
+#endif
                public void BeginAcceptSocketUdp ()
                {
                        Socket sock = new Socket (AddressFamily.InterNetwork,
@@ -1491,8 +1526,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 ();
@@ -1500,6 +1533,9 @@ namespace MonoTests.System.Net.Sockets
                }
                
                [Test]
+#if FEATURE_NO_BSD_SOCKETS
+               [ExpectedException (typeof (PlatformNotSupportedException))]
+#endif
                public void BeginAcceptSocketBound ()
                {
                        Socket sock = new Socket (AddressFamily.InterNetwork,
@@ -1524,8 +1560,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 ();
@@ -1533,6 +1567,9 @@ namespace MonoTests.System.Net.Sockets
                }
                
                [Test]
+#if FEATURE_NO_BSD_SOCKETS
+               [ExpectedException (typeof (PlatformNotSupportedException))]
+#endif
                public void BeginAcceptSocket ()
                {
                        Socket sock = new Socket (AddressFamily.InterNetwork,
@@ -1608,14 +1645,15 @@ 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 ();
                        }
                }
 
                [Test]
+#if FEATURE_NO_BSD_SOCKETS
+               [ExpectedException (typeof (PlatformNotSupportedException))]
+#endif
                public void BeginAcceptSocketAccClosed ()
                {
                        Socket sock = new Socket (AddressFamily.InterNetwork,
@@ -1638,8 +1676,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 ();
                        }
@@ -1667,6 +1703,9 @@ namespace MonoTests.System.Net.Sockets
                }
                
                [Test]
+#if FEATURE_NO_BSD_SOCKETS
+               [ExpectedException (typeof (PlatformNotSupportedException))]
+#endif
                public void BeginConnectAddressPort ()
                {
                        Socket sock = new Socket (AddressFamily.InterNetwork,
@@ -1710,14 +1749,15 @@ namespace MonoTests.System.Net.Sockets
                                                   sock);
                                Assert.Fail ("BeginConnectAddressPortNull #1");
                        } catch (ArgumentNullException) {
-                       } catch {
-                               Assert.Fail ("BeginConnectAddressPortNull #2");
                        } finally {
                                sock.Close ();
                        }
                }
 
                [Test]
+#if FEATURE_NO_BSD_SOCKETS
+               [ExpectedException (typeof (PlatformNotSupportedException))]
+#endif
                public void BeginConnectAddressPortListen ()
                {
                        Socket sock = new Socket (AddressFamily.InterNetwork,
@@ -1733,8 +1773,6 @@ namespace MonoTests.System.Net.Sockets
                                sock.BeginConnect (ip, ep.Port, BCCallback, sock);
                                Assert.Fail ("BeginConnectAddressPortListen #1");
                        } catch (InvalidOperationException) {
-                       } catch {
-                               Assert.Fail ("BeginConnectAddressPortListen #2");
                        } finally {
                                sock.Close ();
                        }
@@ -1814,6 +1852,9 @@ namespace MonoTests.System.Net.Sockets
                }
 
                [Test]
+#if FEATURE_NO_BSD_SOCKETS
+               [ExpectedException (typeof (PlatformNotSupportedException))]
+#endif
                public void BeginConnectMultiple2 ()
                {
                        Socket sock = new Socket (AddressFamily.InterNetwork,
@@ -1880,14 +1921,15 @@ namespace MonoTests.System.Net.Sockets
                                                   sock);
                                Assert.Fail ("BeginConnectMultipleNull #1");
                        } catch (ArgumentNullException) {
-                       } catch {
-                               Assert.Fail ("BeginConnectMultipleNull #2");
                        } finally {
                                sock.Close ();
                        }
                }
 
                [Test]
+#if FEATURE_NO_BSD_SOCKETS
+               [ExpectedException (typeof (PlatformNotSupportedException))]
+#endif
                public void BeginConnectMultipleListen ()
                {
                        Socket sock = new Socket (AddressFamily.InterNetwork,
@@ -1910,8 +1952,6 @@ namespace MonoTests.System.Net.Sockets
                                                   sock);
                                Assert.Fail ("BeginConnectMultipleListen #1");
                        } catch (InvalidOperationException) {
-                       } catch {
-                               Assert.Fail ("BeginConnectMultipleListen #2");
                        } finally {
                                sock.Close ();
                        }
@@ -1948,14 +1988,15 @@ namespace MonoTests.System.Net.Sockets
                                                   BCCallback, sock);
                                Assert.Fail ("BeginConnectHostPort #1");
                        } catch (ArgumentNullException) {
-                       } catch {
-                               Assert.Fail ("BeginConnectHostPort #2");
                        } finally {
                                sock.Close ();
                        }
                }
 
                [Test]
+#if FEATURE_NO_BSD_SOCKETS
+               [ExpectedException (typeof (PlatformNotSupportedException))]
+#endif
                public void BeginConnectHostPortListen ()
                {
                        Socket sock = new Socket (AddressFamily.InterNetwork,
@@ -1972,8 +2013,6 @@ namespace MonoTests.System.Net.Sockets
                                                   BCCallback, sock);
                                Assert.Fail ("BeginConnectHostPortListen #1");
                        } catch (InvalidOperationException) {
-                       } catch {
-                               Assert.Fail ("BeginConnectHostPortListen #2");
                        } finally {
                                sock.Close ();
                        }
@@ -1992,8 +2031,6 @@ namespace MonoTests.System.Net.Sockets
                                                   sock);
                                Assert.Fail ("BeginConnectHostPortNotIP #1");
                        } catch (NotSupportedException) {
-                       } catch {
-                               Assert.Fail ("BeginConnectHostPortNotIP #2");
                        } finally {
                                sock.Close ();
                        }
@@ -2027,6 +2064,9 @@ namespace MonoTests.System.Net.Sockets
                
                [Test]
                [Category ("NotDotNet")] // "Needs XP or later"
+#if FEATURE_NO_BSD_SOCKETS
+               [ExpectedException (typeof (PlatformNotSupportedException))]
+#endif
                public void BeginDisconnect ()
                {
                        Socket sock = new Socket (AddressFamily.InterNetwork,
@@ -2102,8 +2142,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 ();
                        }
@@ -2125,6 +2163,9 @@ namespace MonoTests.System.Net.Sockets
                }
                
                [Test]
+#if FEATURE_NO_BSD_SOCKETS
+               [ExpectedException (typeof (PlatformNotSupportedException))]
+#endif
                public void BindTwice ()
                {
                        Socket sock = new Socket (AddressFamily.InterNetwork,
@@ -2142,14 +2183,15 @@ 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 ();
                        }
                }
                
                [Test]
+#if FEATURE_NO_BSD_SOCKETS
+               [ExpectedException (typeof (PlatformNotSupportedException))]
+#endif
                public void Close ()
                {
                        Socket sock = new Socket (AddressFamily.InterNetwork,
@@ -2178,6 +2220,9 @@ namespace MonoTests.System.Net.Sockets
                }
                
                [Test]
+#if FEATURE_NO_BSD_SOCKETS
+               [ExpectedException (typeof (PlatformNotSupportedException))]
+#endif
                public void ConnectAddressPort ()
                {
                        Socket sock = new Socket (AddressFamily.InterNetwork,
@@ -2212,14 +2257,15 @@ namespace MonoTests.System.Net.Sockets
                                sock.Connect (ip, 1249);
                                Assert.Fail ("ConnectAddressPortNull #1");
                        } catch (ArgumentNullException) {
-                       } catch {
-                               Assert.Fail ("ConnectAddressPortNull #2");
                        } finally {
                                sock.Close ();
                        }
                }
 
                [Test]
+#if FEATURE_NO_BSD_SOCKETS
+               [ExpectedException (typeof (PlatformNotSupportedException))]
+#endif
                public void ConnectAddressPortListen ()
                {
                        Socket sock = new Socket (AddressFamily.InterNetwork,
@@ -2235,8 +2281,6 @@ namespace MonoTests.System.Net.Sockets
                                sock.Connect (ip, ep.Port);
                                Assert.Fail ("ConnectAddressPortListen #1");
                        } catch (InvalidOperationException) {
-                       } catch {
-                               Assert.Fail ("ConnectAddressPortListen #2");
                        } finally {
                                sock.Close ();
                        }
@@ -2308,6 +2352,9 @@ namespace MonoTests.System.Net.Sockets
                }
 
                [Test]
+#if FEATURE_NO_BSD_SOCKETS
+               [ExpectedException (typeof (PlatformNotSupportedException))]
+#endif
                public void ConnectMultiple2 ()
                {
                        Socket sock = new Socket (AddressFamily.InterNetwork,
@@ -2359,14 +2406,15 @@ namespace MonoTests.System.Net.Sockets
                                sock.Connect (ips, 1251);
                                Assert.Fail ("ConnectMultipleNull #1");
                        } catch (ArgumentNullException) {
-                       } catch {
-                               Assert.Fail ("ConnectMultipleNull #2");
                        } finally {
                                sock.Close ();
                        }
                }
 
                [Test]
+#if FEATURE_NO_BSD_SOCKETS
+               [ExpectedException (typeof (PlatformNotSupportedException))]
+#endif
                public void ConnectMultipleListen ()
                {
                        Socket sock = new Socket (AddressFamily.InterNetwork,
@@ -2388,8 +2436,6 @@ namespace MonoTests.System.Net.Sockets
                                sock.Connect (ips, ep.Port);
                                Assert.Fail ("ConnectMultipleListen #1");
                        } catch (InvalidOperationException) {
-                       } catch {
-                               Assert.Fail ("ConnectMultipleListen #2");
                        } finally {
                                sock.Close ();
                        }
@@ -2425,14 +2471,15 @@ namespace MonoTests.System.Net.Sockets
                                sock.Connect ((string)null, 0);
                                Assert.Fail ("ConnectHostPort #1");
                        } catch (ArgumentNullException) {
-                       } catch {
-                               Assert.Fail ("ConnectHostPort #2");
                        } finally {
                                sock.Close ();
                        }
                }
 
                [Test]
+#if FEATURE_NO_BSD_SOCKETS
+               [ExpectedException (typeof (PlatformNotSupportedException))]
+#endif
                public void ConnectHostPortListen ()
                {
                        Socket sock = new Socket (AddressFamily.InterNetwork,
@@ -2448,8 +2495,6 @@ namespace MonoTests.System.Net.Sockets
                                sock.Connect ("localhost", ep.Port);
                                Assert.Fail ("ConnectHostPortListen #1");
                        } catch (InvalidOperationException) {
-                       } catch {
-                               Assert.Fail ("ConnectHostPortListen #2");
                        } finally {
                                sock.Close ();
                        }
@@ -2467,15 +2512,17 @@ namespace MonoTests.System.Net.Sockets
                                sock.Connect ("localhost", 0);
                                Assert.Fail ("ConnectHostPortNotIP #1");
                        } catch (NotSupportedException) {
-                       } catch {
-                               Assert.Fail ("ConnectHostPortNotIP #2");
                        } finally {
                                sock.Close ();
                        }
                }
 
                [Test]
+#if FEATURE_NO_BSD_SOCKETS
+               [ExpectedException (typeof (PlatformNotSupportedException))]
+#else
                [ExpectedException (typeof(ObjectDisposedException))]
+#endif
                public void ConnectHostPortClosed ()
                {
                        Socket sock = new Socket (AddressFamily.InterNetwork,
@@ -2489,6 +2536,9 @@ namespace MonoTests.System.Net.Sockets
                
                [Test]
                [Category ("NotDotNet")] // "Needs XP or later"
+#if FEATURE_NO_BSD_SOCKETS
+               [ExpectedException (typeof (PlatformNotSupportedException))]
+#endif
                public void Disconnect ()
                {
                        Socket sock = new Socket (AddressFamily.InterNetwork,
@@ -2546,6 +2596,9 @@ namespace MonoTests.System.Net.Sockets
                }
                
                [Test]
+#if FEATURE_NO_BSD_SOCKETS
+               [ExpectedException (typeof (PlatformNotSupportedException))]
+#endif
                public void ReceiveGeneric ()
                {
                        int i;
@@ -2603,6 +2656,9 @@ namespace MonoTests.System.Net.Sockets
                }
                
                [Test]
+#if FEATURE_NO_BSD_SOCKETS
+               [ExpectedException (typeof (PlatformNotSupportedException))]
+#endif
                public void SendGeneric ()
                {
                        int i;
@@ -2660,6 +2716,78 @@ namespace MonoTests.System.Net.Sockets
                        listensock.Close ();
                }
 
+               [Test]
+#if FEATURE_NO_BSD_SOCKETS
+               [ExpectedException (typeof (AggregateException))] // Something catches the PlatformNotSupportedException and re-throws an AggregateException    
+#endif
+               public void ConcurrentExceedSocketLimit ()
+               {
+                       var tasks = new Task[4];
+                       for (int i = 0; i < 4; i++) {
+                               tasks[i] = Task.Factory.StartNew (() => SendGenericExceedBuffer ());
+                       }
+                       Task.WaitAll (tasks);
+               }
+
+               [Test]
+#if FEATURE_NO_BSD_SOCKETS
+               [ExpectedException (typeof (PlatformNotSupportedException))]
+#endif
+               public void SendGenericExceedBuffer ()
+               {
+                       // Create a buffer larger than the default max.
+                       const int BUFFER_SIZE = 65 * 1024;
+                       int i;
+
+                       IPEndPoint endpoint = new IPEndPoint(IPAddress.Loopback, NetworkHelpers.FindFreePort ());
+
+                       Socket listensock = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
+                       listensock.Bind (endpoint);
+                       listensock.Listen (1);
+
+                       Socket sendsock = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
+                       sendsock.Connect (endpoint);
+
+                       Socket clientsock = listensock.Accept ();
+
+                       byte[] sendbuf = new byte[BUFFER_SIZE];
+
+                       for (i = 0; i < BUFFER_SIZE; i++) {
+                               sendbuf[i] = (byte)i;
+                       }
+
+                       Task sendTask = Task.Factory.StartNew(() => {
+                               int sent = sendsock.Send (sendbuf);
+
+                               Assert.AreEqual (BUFFER_SIZE, sent, "#1");
+                       });
+
+                       byte[] recvbuf = new byte[BUFFER_SIZE];
+
+                       Task recvTask = Task.Factory.StartNew(() => {
+                               int totalReceived = 0;
+                               byte[] buffer = new byte[256];
+                               while (totalReceived < sendbuf.Length) {
+                                       int recvd = clientsock.Receive (buffer, 0, buffer.Length, SocketFlags.None);
+                                       Array.Copy (buffer, 0, recvbuf, totalReceived, recvd);
+                                       totalReceived += recvd;
+                               }
+
+                               Assert.AreEqual (BUFFER_SIZE, totalReceived, "#2");
+                       });
+
+                       Assert.IsTrue (Task.WaitAll (new []{sendTask, recvTask}, 20 * 1000), "#2a");
+
+                       for (i = 0; i < BUFFER_SIZE; i++) {
+                               Assert.AreEqual (recvbuf[i], sendbuf[i],
+                                                "#3/" + i.ToString());
+                       }
+
+                       sendsock.Close ();
+                       clientsock.Close ();
+                       listensock.Close ();
+               }
+
                [Test]
                public void ListenNotBound ()
                {
@@ -2672,8 +2800,6 @@ 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 ();
                        }
@@ -2697,6 +2823,9 @@ namespace MonoTests.System.Net.Sockets
                }
                
                [Test]
+#if FEATURE_NO_BSD_SOCKETS
+               [ExpectedException (typeof (PlatformNotSupportedException))]
+#endif
                public void CloseWhileReceiving ()
                {
                        CWRSocket = new Socket (AddressFamily.InterNetwork,
@@ -3066,6 +3195,9 @@ namespace MonoTests.System.Net.Sockets
                }
 
                [Test] // ReceiveFrom (Byte [], ref EndPoint)
+#if FEATURE_NO_BSD_SOCKETS
+               [ExpectedException (typeof (PlatformNotSupportedException))]
+#endif
                public void ReceiveFrom1_Buffer_Null ()
                {
                        Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
@@ -3107,6 +3239,9 @@ namespace MonoTests.System.Net.Sockets
                }
 
                [Test] // ReceiveFrom (Byte [], ref EndPoint)
+#if FEATURE_NO_BSD_SOCKETS
+               [ExpectedException (typeof (PlatformNotSupportedException))]
+#endif
                public void ReceiveFrom1_Socket_Closed ()
                {
                        Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
@@ -3127,6 +3262,9 @@ namespace MonoTests.System.Net.Sockets
                }
 
                [Test] // ReceiveFrom (Byte [], SocketFlags, ref EndPoint)
+#if FEATURE_NO_BSD_SOCKETS
+               [ExpectedException (typeof (PlatformNotSupportedException))]
+#endif
                public void ReceiveFrom2_Buffer_Null ()
                {
                        Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
@@ -3168,6 +3306,9 @@ namespace MonoTests.System.Net.Sockets
                }
 
                [Test] // ReceiveFrom (Byte [], SocketFlags, ref EndPoint)
+#if FEATURE_NO_BSD_SOCKETS
+               [ExpectedException (typeof (PlatformNotSupportedException))]
+#endif
                public void ReceiveFrom2_Socket_Closed ()
                {
                        Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
@@ -3188,6 +3329,9 @@ namespace MonoTests.System.Net.Sockets
                }
 
                [Test] // ReceiveFrom (Byte [], Int32, SocketFlags, ref EndPoint)
+#if FEATURE_NO_BSD_SOCKETS
+               [ExpectedException (typeof (PlatformNotSupportedException))]
+#endif
                public void ReceiveFrom3_Buffer_Null ()
                {
                        Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
@@ -3230,6 +3374,9 @@ namespace MonoTests.System.Net.Sockets
                }
 
                [Test] // ReceiveFrom (Byte [], Int32, SocketFlags, ref EndPoint)
+#if FEATURE_NO_BSD_SOCKETS
+               [ExpectedException (typeof (PlatformNotSupportedException))]
+#endif
                public void ReceiveFrom3_Size_OutOfRange ()
                {
                        Socket s;
@@ -3271,6 +3418,9 @@ namespace MonoTests.System.Net.Sockets
                }
 
                [Test] // ReceiveFrom (Byte [], Int32, SocketFlags, ref EndPoint)
+#if FEATURE_NO_BSD_SOCKETS
+               [ExpectedException (typeof (PlatformNotSupportedException))]
+#endif
                public void ReceiveFrom3_Socket_Closed ()
                {
                        Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
@@ -3292,6 +3442,9 @@ namespace MonoTests.System.Net.Sockets
                }
 
                [Test] // ReceiveFrom (Byte [], Int32, Int32, SocketFlags, EndPoint)
+#if FEATURE_NO_BSD_SOCKETS
+               [ExpectedException (typeof (PlatformNotSupportedException))]
+#endif
                public void ReceiveFrom4_Buffer_Null ()
                {
                        Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
@@ -3311,6 +3464,9 @@ namespace MonoTests.System.Net.Sockets
                }
 
                [Test] // ReceiveFrom (Byte [], Int32, Int32, SocketFlags, EndPoint)
+#if FEATURE_NO_BSD_SOCKETS
+               [ExpectedException (typeof (PlatformNotSupportedException))]
+#endif
                public void ReceiveFrom4_Offset_OutOfRange ()
                {
                        Socket s;
@@ -3374,6 +3530,9 @@ namespace MonoTests.System.Net.Sockets
                }
 
                [Test] // ReceiveFrom (Byte [], Int32, Int32, SocketFlags, EndPoint)
+#if FEATURE_NO_BSD_SOCKETS
+               [ExpectedException (typeof (PlatformNotSupportedException))]
+#endif
                public void ReceiveFrom4_Size_OutOfRange ()
                {
                        Socket s;
@@ -3432,6 +3591,9 @@ namespace MonoTests.System.Net.Sockets
                }
 
                [Test] // ReceiveFrom (Byte [], Int32, Int32, SocketFlags, ref EndPoint)
+#if FEATURE_NO_BSD_SOCKETS
+               [ExpectedException (typeof (PlatformNotSupportedException))]
+#endif
                public void ReceiveFrom4_Socket_Closed ()
                {
                        Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
@@ -3454,6 +3616,9 @@ namespace MonoTests.System.Net.Sockets
                }
 
                [Test]
+#if FEATURE_NO_BSD_SOCKETS
+               [ExpectedException (typeof (PlatformNotSupportedException))]
+#endif
                public void ReceiveRemoteClosed ()
                {
                        var port = NetworkHelpers.FindFreePort ();
@@ -3481,77 +3646,85 @@ namespace MonoTests.System.Net.Sockets
                //
                // Test case for bug #471580
                [Test]
+#if FEATURE_NO_BSD_SOCKETS
+               [ExpectedException (typeof (PlatformNotSupportedException))]
+#endif
                public void UdpDoubleBind ()
                {
-                       Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
-                       s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, 1);
-                       
-                       var ep = new IPEndPoint (IPAddress.Any, NetworkHelpers.FindFreePort ());
-                       s.Bind (ep);
-                       
-                       Socket ss = new Socket (AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
-                       ss.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, 1);
-                       
-                       ss.Bind (new IPEndPoint (IPAddress.Any, ep.Port));
+                       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 MONOTOUCH
-               // when the linker is enabled then reflection won't work and would throw an NRE
-               // this is also always true for iOS - so we do not need to poke internals
-               static bool SupportsPortReuse ()
-               {
-                       return true;
-               }
-#else
-               static bool? supportsPortReuse;
-               static bool SupportsPortReuse ()
-               {
-                       if (supportsPortReuse.HasValue)
-                               return supportsPortReuse.Value;
+                               if (supportsReuseAddress)
+                                       ss.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
 
-                       supportsPortReuse = (bool) typeof (Socket).GetMethod ("SupportsPortReuse",
-                                       BindingFlags.Static | BindingFlags.NonPublic)
-                                       .Invoke (null, new object [] {});
-                       return supportsPortReuse.Value;
+                               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) {
+                               }
+                       }
                }
-#endif
 
                // Test case for bug #31557
                [Test]
+#if FEATURE_NO_BSD_SOCKETS
+               [ExpectedException (typeof (PlatformNotSupportedException))]
+#endif
                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)) {
-                               s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
+                               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);
 
-                               ss.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
+                               if (supportsReuseAddress)
+                                       ss.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
 
-                               Exception ex = null;
                                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) {
-                                       ex = e;
                                }
-
-                               Assert.AreEqual (SupportsPortReuse (), ex == null);
                        }
                }
 
                [Test]
                [Category ("NotOnMac")]
-                public void ConnectedProperty ()
-                {
+#if FULL_AOT_DESKTOP
+               [Ignore ("https://bugzilla.xamarin.com/show_bug.cgi?id=52157")]
+#endif
+               public void ConnectedProperty ()
+               {
                        TcpListener listener = new TcpListener (IPAddress.Loopback, NetworkHelpers.FindFreePort ());
                        listener.Start();
 
@@ -3753,6 +3926,9 @@ namespace MonoTests.System.Net.Sockets
                }
 
                [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
+#if FEATURE_NO_BSD_SOCKETS
+               [ExpectedException (typeof (PlatformNotSupportedException))]
+#endif
                public void SetSocketOption3_AddMembershipIPv4_IPv6MulticastOption ()
                {
                        IPAddress mcast_addr = IPAddress.Parse ("239.255.255.250");
@@ -3775,6 +3951,9 @@ namespace MonoTests.System.Net.Sockets
                }
 
                [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
+#if FEATURE_NO_BSD_SOCKETS
+               [ExpectedException (typeof (PlatformNotSupportedException))]
+#endif
                public void SetSocketOption3_AddMembershipIPv4_MulticastOption ()
                {
                        IPAddress mcast_addr = IPAddress.Parse ("239.255.255.250");
@@ -3811,6 +3990,9 @@ namespace MonoTests.System.Net.Sockets
                }
 
                [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
+#if FEATURE_NO_BSD_SOCKETS
+               [ExpectedException (typeof (PlatformNotSupportedException))]
+#endif
                public void SetSocketOption3_AddMembershipIPv6_IPv6MulticastOption ()
                {
                        if (!Socket.OSSupportsIPv6)
@@ -3826,6 +4008,9 @@ namespace MonoTests.System.Net.Sockets
                }
 
                [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
+#if FEATURE_NO_BSD_SOCKETS
+               [ExpectedException (typeof (PlatformNotSupportedException))]
+#endif
                public void SetSocketOption3_AddMembershipIPv6_MulticastOption ()
                {
                        if (!Socket.OSSupportsIPv6)
@@ -3980,6 +4165,9 @@ namespace MonoTests.System.Net.Sockets
                }
 
                [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
+#if FEATURE_NO_BSD_SOCKETS
+               [ExpectedException (typeof (PlatformNotSupportedException))]
+#endif
                public void SetSocketOption3_DropMembershipIPv4_IPv6MulticastOption ()
                {
                        IPAddress mcast_addr = IPAddress.Parse ("239.255.255.250");
@@ -4004,6 +4192,9 @@ namespace MonoTests.System.Net.Sockets
                }
 
                [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
+#if FEATURE_NO_BSD_SOCKETS
+               [ExpectedException (typeof (PlatformNotSupportedException))]
+#endif
                public void SetSocketOption3_DropMembershipIPv4_MulticastOption ()
                {
                        IPAddress mcast_addr = IPAddress.Parse ("239.255.255.250");
@@ -4044,6 +4235,9 @@ namespace MonoTests.System.Net.Sockets
                }
 
                [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
+#if FEATURE_NO_BSD_SOCKETS
+               [ExpectedException (typeof (PlatformNotSupportedException))]
+#endif
                public void SetSocketOption3_DropMembershipIPv6_IPv6MulticastOption ()
                {
                        if (!Socket.OSSupportsIPv6)
@@ -4062,6 +4256,9 @@ namespace MonoTests.System.Net.Sockets
                }
 
                [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
+#if FEATURE_NO_BSD_SOCKETS
+               [ExpectedException (typeof (PlatformNotSupportedException))]
+#endif
                public void SetSocketOption3_DropMembershipIPv6_MulticastOption ()
                {
                        if (!Socket.OSSupportsIPv6)
@@ -4148,6 +4345,7 @@ namespace MonoTests.System.Net.Sockets
                }
 
                [Test]
+               [Category ("RequiresBSDSockets")] // on watchOS device this happens: System.Net.Sockets.SocketException : The requested address is not valid in this context. This situation is too complex to detect and throw a PlatformNotSupportedException, so just ignore it.
                public void SetSocketOption_MulticastInterfaceIndex_Any ()
                {
                        IPAddress ip = IPAddress.Parse ("239.255.255.250");
@@ -4160,6 +4358,7 @@ namespace MonoTests.System.Net.Sockets
                }
 
                [Test]
+               [Category ("RequiresBSDSockets")] // on watchOS device this happens: System.Net.Sockets.SocketException : The requested address is not valid in this context. This situation is too complex to detect and throw a PlatformNotSupportedException, so just ignore it.
                public void SetSocketOption_MulticastInterfaceIndex_Loopback ()
                {
                        IPAddress ip = IPAddress.Parse ("239.255.255.250");
@@ -4196,6 +4395,9 @@ namespace MonoTests.System.Net.Sockets
                }
 
                [Test]
+#if FEATURE_NO_BSD_SOCKETS
+               [ExpectedException (typeof (PlatformNotSupportedException))]
+#endif
                public void Shutdown_NoConnect ()
                {
                        Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
@@ -4292,9 +4494,27 @@ namespace MonoTests.System.Net.Sockets
                }
                
                [Test]
+#if FEATURE_NO_BSD_SOCKETS
+               [ExpectedException (typeof (PlatformNotSupportedException))]
+#endif
                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);
@@ -4312,18 +4532,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);
@@ -4334,80 +4555,98 @@ namespace MonoTests.System.Net.Sockets
                }
                
                [Test]
+#if FEATURE_NO_BSD_SOCKETS
+               [ExpectedException (typeof (PlatformNotSupportedException))]
+#endif
                public void ConnectToIPV4EndPointUsingDualModelSocket () {
-                       using (var server = new Socket (SocketType.Stream, ProtocolType.Tcp))
-                       using (var client = new Socket (SocketType.Stream, ProtocolType.Tcp)) {
+                       /*
+                        * IPv6 DualMode sockets are defaults in Mono. Explicitly specify that
+                        * anyways in this test to make it more interoparable with .NET where
+                        * IPv6 and DualMode needs to be specified.
+                        */
+                       using (var server = new Socket (AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp)) {
+
                                var host = new IPEndPoint (IPAddress.Loopback, NetworkHelpers.FindFreePort ());
-                                       
+
+                               server.DualMode = true;
                                server.Bind (host);
-                               server.Listen (0);
+                               /*
+                                * Nothing to Accept the connect - we need a backlog to make sure we don't get 
+                                Connection refused.
+                                */
+                               server.Listen (3);
                                
                                var ep = server.LocalEndPoint as IPEndPoint;
-                               
+                               var client = new Socket (AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp);
+                               client.DualMode = true;
                                client.Connect (ep);
-                               client.Disconnect (true);
-                               
+                               client.Disconnect (false);
+                               client.Close ();
+
+                               client = new Socket (AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp);
+                               client.DualMode = true;
                                client.Connect (IPAddress.Loopback, ep.Port);
-                               client.Disconnect (true);
-                               
-                               client.Connect (new [] {IPAddress.Loopback}, ep.Port);
-                               client.Disconnect (true);
+                               client.Disconnect (false);
+                               client.Close ();
+
+                               client = new Socket (AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp);
+                               client.DualMode = true;
+                               client.Connect (new [] { IPAddress.Loopback }, ep.Port);
+                               client.Disconnect (false);
+                               client.Close ();
                        }
                }
                
                [Test]
+#if FEATURE_NO_BSD_SOCKETS
+               [ExpectedException (typeof (PlatformNotSupportedException))]
+#endif
                public void BeginConnectToIPV4EndPointUsingDualModelSocket () {
-                       using (var server = new Socket (SocketType.Stream, ProtocolType.Tcp))
-                       using (var client = new Socket (SocketType.Stream, ProtocolType.Tcp)) {
+                       /*
+                        * IPv6 DualMode sockets are defaults in Mono. Explicitly specify that
+                        * anyways in this test to make it more interoparable with .NET where
+                        * IPv6 and DualMode needs to be specified.
+                        */
+                       using (var server = new Socket (AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp))
+                       {
                                var host = new IPEndPoint (IPAddress.Loopback, NetworkHelpers.FindFreePort ());
-                                       
+
+                               server.DualMode = true;
                                server.Bind (host);
-                               server.Listen (0);
+                               server.Listen (10);
                                
                                var ep = server.LocalEndPoint as IPEndPoint;
-                               
+
                                BCCalledBack.Reset ();
+                               var client = new Socket (AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp);
+                               client.DualMode = true;
                                var ar1 = client.BeginConnect (ep, BCCallback, client);
                                Assert.IsTrue (BCCalledBack.WaitOne (10000), "#1");
-                               client.Disconnect (true);
-                               
+                               client.Disconnect (false);
+                               client.Close ();
+
                                BCCalledBack.Reset ();
+                               client = new Socket (AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp);
+                               client.DualMode = true;
                                var ar2 = client.BeginConnect (IPAddress.Loopback, ep.Port, BCCallback, client);
                                Assert.IsTrue (BCCalledBack.WaitOne (10000), "#2");
-                               client.Disconnect (true);
-                               
+                               client.Disconnect (false);
+                               client.Close ();
+
                                BCCalledBack.Reset ();
+                               client = new Socket (AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp);
+                               client.DualMode = true;
                                var ar3 = client.BeginConnect (new [] {IPAddress.Loopback}, ep.Port, BCCallback, client);
                                Assert.IsTrue (BCCalledBack.WaitOne (10000), "#2");
-                               client.Disconnect (true);
+                               client.Disconnect (false);
+                               client.Close();
                        }
                }
 
-               Socket StartSocketServer ()
-               {
-
-                       Socket listenSocket = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
-                       
-                       listenSocket.Bind (new IPEndPoint (IPAddress.Loopback, NetworkHelpers.FindFreePort ()));
-                       listenSocket.Listen (1);
-
-                       listenSocket.BeginAccept (new AsyncCallback (ReceiveCallback), listenSocket);
-                       
-                       return listenSocket;
-               }
-
-               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]
+#if FEATURE_NO_BSD_SOCKETS
+               [ExpectedException (typeof (PlatformNotSupportedException))]
+#endif
                public void UdpMulticasTimeToLive ()
                {
                        /* see https://bugzilla.xamarin.com/show_bug.cgi?id=36941 */
@@ -4419,6 +4658,46 @@ namespace MonoTests.System.Net.Sockets
                                socket.SetSocketOption (SocketOptionLevel.IP, SocketOptionName.MulticastTimeToLive, 19);
                        }
                }
+
+               [Test] // Covers 41616
+               public void ConnectAsyncUnhandledEx ()
+               {
+                       var mre = new ManualResetEvent (false);
+
+                       var endPoint = new IPEndPoint(0,0);
+                       var socket = new Socket(endPoint.AddressFamily, SocketType.Stream, ProtocolType.Unspecified);
+
+                       var socketArgs = new SocketAsyncEventArgs();
+                       socketArgs.RemoteEndPoint = endPoint;
+                       socketArgs.Completed += (sender, e) => mre.Set ();
+
+                       socket.ConnectAsync (socketArgs);
+
+                       Assert.IsTrue (mre.WaitOne (1000), "ConnectedAsync timeout");
+               }
+
+               [Test] // Covers https://bugzilla.xamarin.com/show_bug.cgi?id=52549
+#if FEATURE_NO_BSD_SOCKETS
+               [ExpectedException (typeof (PlatformNotSupportedException))]
+#endif
+               public void SocketMismatchProtocol ()
+               {
+                       try {
+                               using (Socket socket = new Socket (AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Tcp));
+                               Assert.Fail ("#1");
+                       } catch (SocketException e) {
+                               // Only work on OSX
+                               // Assert.AreEqual(SocketError.ProtocolType, e.SocketErrorCode, "#2");
+                       }
+
+                       try {
+                               using (Socket socket = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Udp));
+                               Assert.Fail ("#3");
+                       } catch (SocketException e) {
+                               // Only work on OSX
+                               // Assert.AreEqual(SocketError.ProtocolType, e.SocketErrorCode, "#4");
+                       }
+               }
        }
 }