Merge pull request #439 from mono-soc-2012/garyb/iconfix
[mono.git] / mcs / class / System / Test / System.Net.Sockets / SocketTest.cs
1 // System.Net.Sockets.SocketTest.cs
2 //
3 // Authors:
4 //    Brad Fitzpatrick (brad@danga.com)
5 //    Gonzalo Paniagua Javier (gonzalo@novell.com)
6 //
7 // (C) Copyright 2003 Brad Fitzpatrick
8 // Copyright (c) 2005 Novell, Inc. (http://www.novell.com)
9 //
10
11 using System;
12 using System.Collections;
13 using System.Threading;
14 using System.Net;
15 using System.Net.Sockets;
16 using NUnit.Framework;
17
18 #if NET_2_0
19 using System.Collections.Generic;
20 #endif
21
22 namespace MonoTests.System.Net.Sockets
23 {
24         [TestFixture]
25         public class SocketTest
26         {
27                 // note: also used in SocketCas tests
28                 public const string BogusAddress = "192.168.244.244";
29                 public const int BogusPort = 23483;
30
31                 [Test]
32                 public void ConnectIPAddressAny ()
33                 {
34                         IPEndPoint ep = new IPEndPoint (IPAddress.Any, 0);
35
36                         /* UDP sockets use Any to disconnect
37                         try {
38                                 using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)) {
39                                         s.Connect (ep);
40                                         s.Close ();
41                                 }
42                                 Assert.Fail ("#1");
43                         } catch (SocketException ex) {
44                                 Assert.AreEqual (10049, ex.ErrorCode, "#2");
45                         }
46                         */
47
48                         try {
49                                 using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
50                                         s.Connect (ep);
51                                         s.Close ();
52                                 }
53                                 Assert.Fail ("#3");
54                         } catch (SocketException ex) {
55                                 Assert.AreEqual (10049, ex.ErrorCode, "#4");
56                         }
57                 }
58
59                 [Test]
60                 [Ignore ("Bug #75158")] // Looks like MS fails after the .ctor, when you try to use the socket
61                 public void IncompatibleAddress ()
62                 {
63                         IPEndPoint epIPv6 = new IPEndPoint (IPAddress.IPv6Any,
64                                                                 0);
65
66                         try {
67                                 using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP)) {
68                                         s.Connect (epIPv6);
69                                         s.Close ();
70                                 }
71                                 Assert.Fail ("#1");
72                         } catch (SocketException ex) {
73 #if !NET_2_0
74                                 // invalid argument
75                                 int expectedError = 10022;
76 #else
77                                 // address incompatible with protocol
78                                 int expectedError = 10047;
79 #endif
80                                 Assert.AreEqual (expectedError, ex.ErrorCode,
81                                                 "#2");
82                         }
83                 }
84
85                 [Test]
86                 [Category ("InetAccess")]
87                 public void BogusEndConnect ()
88                 {
89                         IPAddress ipOne = IPAddress.Parse (BogusAddress);
90                         IPEndPoint ipEP = new IPEndPoint (ipOne, BogusPort);
91                         Socket sock = new Socket (ipEP.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
92                         IAsyncResult ar = sock.BeginConnect (ipEP, null, null);
93
94                         try {
95                                 // should raise an exception because connect was bogus
96                                 sock.EndConnect (ar);
97                                 Assert.Fail ("#1");
98                         } catch (SocketException ex) {
99                                 // Actual error code depends on network configuration.
100                                 var error = (SocketError) ex.ErrorCode;
101                                 Assert.That (error == SocketError.TimedOut ||
102                                              error == SocketError.ConnectionRefused ||
103                                              error == SocketError.NetworkUnreachable ||
104                                              error == SocketError.HostUnreachable, "#2");
105                         }
106                 }
107
108                 [Test]
109                 [ExpectedException (typeof (ArgumentNullException))]
110                 public void SelectEmpty ()
111                 {
112                         ArrayList list = new ArrayList ();
113                         Socket.Select (list, list, list, 1000);
114                 }
115                 
116                 private bool BlockingConnect (bool block)
117                 {
118                         IPEndPoint ep = new IPEndPoint(IPAddress.Loopback, 1234);
119                         Socket server = new Socket(AddressFamily.InterNetwork,
120                                                    SocketType.Stream,
121                                                    ProtocolType.Tcp);
122                         server.Bind(ep);
123                         server.Blocking=block;
124
125                         server.Listen(0);
126
127                         Socket conn = new Socket (AddressFamily.InterNetwork,
128                                                   SocketType.Stream,
129                                                   ProtocolType.Tcp);
130                         conn.Connect (ep);
131
132                         Socket client = server.Accept();
133                         bool client_block = client.Blocking;
134
135                         client.Close();
136                         conn.Close();
137                         server.Close();
138                         
139                         return(client_block);
140                 }
141
142                 [Test]
143                 public void AcceptBlockingStatus()
144                 {
145                         bool block;
146
147                         block = BlockingConnect(true);
148                         Assert.AreEqual (block, true, "BlockingStatus01");
149
150                         block = BlockingConnect(false);
151                         Assert.AreEqual (block, false, "BlockingStatus02");
152                 }
153
154                 static bool CFAConnected = false;
155                 static ManualResetEvent CFACalledBack;
156                 
157                 private static void CFACallback (IAsyncResult asyncResult)
158                 {
159                         Socket sock = (Socket)asyncResult.AsyncState;
160                         CFAConnected = sock.Connected;
161                         
162                         if (sock.Connected) {
163                                 sock.EndConnect (asyncResult);
164                         }
165
166                         CFACalledBack.Set ();
167                 }
168
169                 [Test] // Connect (IPEndPoint)
170                 public void Connect1_RemoteEP_Null ()
171                 {
172                         Socket s = new Socket (AddressFamily.InterNetwork,
173                                 SocketType.Stream, ProtocolType.Tcp);
174                         try {
175                                 s.Connect ((IPEndPoint) null);
176                                 Assert.Fail ("#1");
177                         } catch (ArgumentNullException ex) {
178                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
179                                 Assert.IsNull (ex.InnerException, "#3");
180                                 Assert.IsNotNull (ex.Message, "#4");
181                                 Assert.AreEqual ("remoteEP", ex.ParamName, "#5");
182                         }
183                 }
184
185                 [Test]
186                 public void ConnectFailAsync ()
187                 {
188                         Socket sock = new Socket (AddressFamily.InterNetwork,
189                                                   SocketType.Stream,
190                                                   ProtocolType.Tcp);
191                         sock.Blocking = false;
192                         CFACalledBack = new ManualResetEvent (false);
193                         CFACalledBack.Reset ();
194
195                         /* Need a port that is not being used for
196                          * anything...
197                          */
198                         sock.BeginConnect (new IPEndPoint (IPAddress.Loopback,
199                                                            114),
200                                            new AsyncCallback (CFACallback),
201                                            sock);
202                         CFACalledBack.WaitOne ();
203
204                         Assert.AreEqual (CFAConnected, false, "ConnectFail");
205                 }
206                 
207 #if !TARGET_JVM
208                 [Test]
209 #if !NET_2_0
210                 [ExpectedException (typeof (ArgumentException))]
211 #endif
212                 public void SetSocketOptionBoolean ()
213                 {
214                         IPEndPoint ep = new IPEndPoint (IPAddress.Loopback, 1);
215                         Socket sock = new Socket (ep.Address.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
216                         try {
217                                 sock.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);
218                         } finally {
219                                 sock.Close ();
220                         }
221                 }
222 #endif
223                 [Test]
224                 public void TestSelect1 ()
225                 {
226                         Socket srv = CreateServer ();
227                         ClientSocket clnt = new ClientSocket (srv.LocalEndPoint);
228                         Thread th = new Thread (new ThreadStart (clnt.ConnectSleepClose));
229                         Socket acc = null;
230                         try {
231                                 th.Start ();
232                                 acc = srv.Accept ();
233                                 clnt.Write ();
234                                 ArrayList list = new ArrayList ();
235                                 ArrayList empty = new ArrayList ();
236                                 list.Add (acc);
237                                 Socket.Select (list, empty, empty, 100);
238                                 Assert.AreEqual (0, empty.Count, "#01");
239                                 Assert.AreEqual (1, list.Count, "#02");
240                                 Socket.Select (empty, list, empty, 100);
241                                 Assert.AreEqual (0, empty.Count, "#03");
242                                 Assert.AreEqual (1, list.Count, "#04");
243                                 Socket.Select (list, empty, empty, -1);
244                                 Assert.AreEqual (0, empty.Count, "#05");
245                                 Assert.AreEqual (1, list.Count, "#06");
246                                 // Need to read the 10 bytes from the client to avoid a RST
247                                 byte [] bytes = new byte [10];
248                                 acc.Receive (bytes);
249                         } finally {
250                                 if (acc != null)
251                                         acc.Close ();
252                                 srv.Close ();
253                         }
254                 }
255
256                 static Socket CreateServer ()
257                 {
258                         Socket sock = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
259                         sock.Bind (new IPEndPoint (IPAddress.Loopback, 0));
260                         sock.Listen (1);
261                         return sock;
262                 }
263
264                 class ClientSocket {
265                         Socket sock;
266                         EndPoint ep;
267
268                         public ClientSocket (EndPoint ep)
269                         {
270                                 this.ep = ep;
271                                 sock = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
272                         }
273
274                         public void ConnectSleepClose ()
275                         {
276                                 sock.Connect (ep);
277                                 Thread.Sleep (2000);
278                                 sock.Close ();
279                         }
280
281                         public void Write ()
282                         {
283                                 byte [] b = new byte [10];
284                                 sock.Send (b);
285                         }
286                 }
287
288                 byte[] buf = new byte[100];
289
290                 [Test]
291                 [ExpectedException (typeof (ObjectDisposedException))]
292                 public void Disposed2 ()
293                 {
294                         Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
295                         s.Close();
296
297                         s.Blocking = true;
298                 }
299
300                 [Test]
301                 [ExpectedException (typeof (ObjectDisposedException))]
302                 public void Disposed6 ()
303                 {
304                         Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
305                         s.Close();
306
307                         s.Listen (5);
308                 }
309
310                 [Test]
311                 [ExpectedException (typeof (ObjectDisposedException))]
312                 public void Disposed7 ()
313                 {
314                         Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
315                         s.Close();
316
317                         s.Poll (100, 0);
318                 }
319
320                 [Test]
321                 [ExpectedException (typeof (ObjectDisposedException))]
322                 public void Disposed15 ()
323                 {
324                         Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
325                         s.Close();
326
327                         s.Send (buf);
328                 }
329
330                 [Test]
331                 [ExpectedException (typeof (ObjectDisposedException))]
332                 public void Disposed16 ()
333                 {
334                         Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
335                         s.Close();
336
337                         s.Send (buf, 0);
338                 }
339
340                 [Test]
341                 [ExpectedException (typeof (ObjectDisposedException))]
342                 public void Disposed17 ()
343                 {
344                         Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
345                         s.Close();
346
347                         s.Send (buf, 10, 0);
348                 }
349
350                 [Test]
351                 [ExpectedException (typeof (ObjectDisposedException))]
352                 public void Disposed18 ()
353                 {
354                         Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
355                         s.Close();
356
357                         s.Send (buf, 0, 10, 0);
358                 }
359
360                 [Test]
361                 [ExpectedException (typeof (ObjectDisposedException))]
362                 public void Disposed19 ()
363                 {
364                         Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
365                         EndPoint ep = new IPEndPoint (IPAddress.Any, 31337);
366                         s.Close();
367
368                         s.SendTo (buf, 0, ep);
369                 }
370
371                 [Test]
372                 [ExpectedException (typeof (ObjectDisposedException))]
373                 public void Disposed20 ()
374                 {
375                         Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
376                         EndPoint ep = new IPEndPoint (IPAddress.Any, 31337);
377                         s.Close();
378
379                         s.SendTo (buf, 10, 0, ep);
380                 }
381
382                 [Test]
383                 [ExpectedException (typeof (ObjectDisposedException))]
384                 public void Disposed21 ()
385                 {
386                         Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
387                         EndPoint ep = new IPEndPoint (IPAddress.Any, 31337);
388                         s.Close();
389
390                         s.SendTo (buf, 0, 10, 0, ep);
391                 }
392
393                 [Test]
394                 [ExpectedException (typeof (ObjectDisposedException))]
395                 public void Disposed22 ()
396                 {
397                         Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
398                         EndPoint ep = new IPEndPoint (IPAddress.Any, 31337);
399                         s.Close();
400
401                         s.SendTo (buf, ep);
402                 }
403
404                 [Test]
405                 [ExpectedException (typeof (ObjectDisposedException))]
406                 public void Disposed23 ()
407                 {
408                         Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
409                         s.Close();
410
411                         s.Shutdown (0);
412                 }
413
414                 [Test]
415                 public void GetHashCodeTest ()
416                 {
417                         Socket server = new Socket (AddressFamily.InterNetwork,
418                                 SocketType.Stream, ProtocolType.Tcp);
419                         IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
420                                                         9010);
421                         server.Bind (ep);
422                         server.Listen (1);
423
424                         Socket client = new Socket (AddressFamily.InterNetwork, 
425                                 SocketType.Stream, ProtocolType.Tcp);
426                         int hashcodeA = client.GetHashCode ();
427                         client.Connect (ep);
428                         int hashcodeB = client.GetHashCode ();
429                         Assert.AreEqual (hashcodeA, hashcodeB, "#1");
430                         client.Close ();
431                         int hashcodeC = client.GetHashCode ();
432 #if NET_2_0
433                         Assert.AreEqual (hashcodeB, hashcodeC, "#2");
434 #else
435                         Assert.IsFalse (hashcodeB == hashcodeC, "#2");
436 #endif
437                         server.Close ();
438                 }
439
440                 static ManualResetEvent SocketError_event = new ManualResetEvent (false);
441
442                 private static void SocketError_callback (IAsyncResult ar)
443                 {
444                         Socket sock = (Socket)ar.AsyncState;
445                         
446                         if(sock.Connected) {
447                                 sock.EndConnect (ar);
448                         }
449
450                         SocketError_event.Set ();
451                 }
452
453                 [Test]
454                 public void SocketErrorTest ()
455                 {
456                         Socket sock = new Socket (AddressFamily.InterNetwork,
457                                                   SocketType.Stream,
458                                                   ProtocolType.Tcp);
459                         IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
460                                                         BogusPort);
461                         
462                         SocketError_event.Reset ();
463
464                         sock.Blocking = false;
465                         sock.BeginConnect (ep, new AsyncCallback(SocketError_callback),
466                                 sock);
467
468                         if (SocketError_event.WaitOne (2000, false) == false) {
469                                 Assert.Fail ("SocketError wait timed out");
470                         }
471
472                         Assert.AreEqual (false, sock.Connected, "SocketError #1");
473
474                         int error;
475
476                         error = (int)sock.GetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Error);
477                         Assert.AreEqual (10061, error, "SocketError #2");
478
479                         error = (int)sock.GetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Error);
480                         Assert.AreEqual (10061, error, "SocketError #3");
481
482                         sock.Close ();
483                 }
484                 
485
486 #if NET_2_0
487                 [Test]
488                 public void SocketInformationCtor ()
489                 {
490                 }
491                 
492                 [Test]
493                 public void DontFragmentDefaultTcp ()
494                 {
495                         Socket sock = new Socket (AddressFamily.InterNetwork,
496                                                   SocketType.Stream,
497                                                   ProtocolType.Tcp);
498                         
499                         Assert.AreEqual (false, sock.DontFragment, "DontFragmentDefaultTcp");
500
501                         sock.Close ();
502                 }
503
504                 [Test]
505                 [Category ("NotOnMac")] // DontFragment doesn't work on Mac
506                 public void DontFragmentChangeTcp ()
507                 {
508                         Socket sock = new Socket (AddressFamily.InterNetwork,
509                                                   SocketType.Stream,
510                                                   ProtocolType.Tcp);
511                         
512                         sock.DontFragment = true;
513                         
514                         Assert.AreEqual (true, sock.DontFragment, "DontFragmentChangeTcp");
515
516                         sock.Close ();
517                 }
518                 
519                 [Test]
520                 public void DontFragmentDefaultUdp ()
521                 {
522                         Socket sock = new Socket (AddressFamily.InterNetwork,
523                                                   SocketType.Dgram,
524                                                   ProtocolType.Udp);
525                         
526                         Assert.AreEqual (false, sock.DontFragment, "DontFragmentDefaultUdp");
527
528                         sock.Close ();
529                 }
530
531                 [Test]
532                 [Category ("NotOnMac")] // DontFragment doesn't work on Mac
533                 public void DontFragmentChangeUdp ()
534                 {
535                         Socket sock = new Socket (AddressFamily.InterNetwork,
536                                                   SocketType.Dgram,
537                                                   ProtocolType.Udp);
538                         
539                         sock.DontFragment = true;
540                         
541                         Assert.AreEqual (true, sock.DontFragment, "DontFragmentChangeUdp");
542
543                         sock.Close ();
544                 }
545
546                 [Test]
547                 [ExpectedException (typeof(ObjectDisposedException))]
548                 public void DontFragmentClosed ()
549                 {
550                         Socket sock = new Socket (AddressFamily.InterNetwork,
551                                                   SocketType.Stream,
552                                                   ProtocolType.Tcp);
553                         
554                         sock.Close ();
555                         
556                         bool val = sock.DontFragment;
557                 }
558                 
559                 [Test]
560                 [Category ("NotWorking")] // Need to pick a non-IP AddressFamily that "works" on both mono and ms, this one only works on ms
561                 public void DontFragment ()
562                 {
563                         Socket sock = new Socket (AddressFamily.NetBios,
564                                                   SocketType.Seqpacket,
565                                                   ProtocolType.Unspecified);
566                         
567                         try {
568                                 sock.DontFragment = true;
569                                 Assert.Fail ("DontFragment #1");
570                         } catch (NotSupportedException) {
571                         } catch {
572                                 Assert.Fail ("DontFragment #2");
573                         } finally {
574                                 sock.Close ();
575                         }
576                 }
577                 
578                 [Test]
579                 public void EnableBroadcastDefaultTcp ()
580                 {
581                         Socket sock = new Socket (AddressFamily.InterNetwork,
582                                                   SocketType.Stream,
583                                                   ProtocolType.Tcp);
584                         
585                         try {
586                                 bool value = sock.EnableBroadcast;
587                                 Assert.Fail ("EnableBroadcastDefaultTcp #1");
588                         } catch (SocketException ex) {
589                                 Assert.AreEqual (10042, ex.ErrorCode, "EnableBroadcastDefaultTcp #2");
590                         } catch {
591                                 Assert.Fail ("EnableBroadcastDefaultTcp #2");
592                         } finally {
593                                 sock.Close ();
594                         }
595                 }
596
597                 [Test]
598                 public void EnableBroadcastDefaultUdp ()
599                 {
600                         Socket sock = new Socket (AddressFamily.InterNetwork,
601                                                   SocketType.Dgram,
602                                                   ProtocolType.Udp);
603                         
604                         Assert.AreEqual (false, sock.EnableBroadcast, "EnableBroadcastDefaultUdp");
605
606                         sock.Close ();
607                 }
608                 
609                 [Test]
610                 public void EnableBroadcastChangeTcp ()
611                 {
612                         Socket sock = new Socket (AddressFamily.InterNetwork,
613                                                   SocketType.Stream,
614                                                   ProtocolType.Tcp);
615                         
616                         try {
617                                 sock.EnableBroadcast = true;
618                                 Assert.Fail ("EnableBroadcastChangeTcp #1");
619                         } catch (SocketException ex) {
620                                 Assert.AreEqual (10042, ex.ErrorCode, "EnableBroadcastChangeTcp #2");
621                         } catch {
622                                 Assert.Fail ("EnableBroadcastChangeTcp #2");
623                         } finally {
624                                 sock.Close ();
625                         }
626                 }
627                 
628                 [Test]
629                 public void EnableBroadcastChangeUdp ()
630                 {
631                         Socket sock = new Socket (AddressFamily.InterNetwork,
632                                                   SocketType.Dgram,
633                                                   ProtocolType.Udp);
634                         
635                         sock.EnableBroadcast = true;
636                         
637                         Assert.AreEqual (true, sock.EnableBroadcast, "EnableBroadcastChangeUdp");
638
639                         sock.Close ();
640                 }
641
642                 [Test]
643                 [ExpectedException (typeof(ObjectDisposedException))]
644                 public void EnableBroadcastClosed ()
645                 {
646                         Socket sock = new Socket (AddressFamily.InterNetwork,
647                                                   SocketType.Dgram,
648                                                   ProtocolType.Udp);
649                         
650                         sock.Close ();
651                         
652                         bool val = sock.EnableBroadcast;
653                 }
654
655                 /* Can't test the default for ExclusiveAddressUse as
656                  * it's different on different versions and service
657                  * packs of windows
658                  */
659                 [Test]
660                 [Category ("NotWorking")] // Not supported on Linux
661                 public void ExclusiveAddressUseUnbound ()
662                 {
663                         Socket sock = new Socket (AddressFamily.InterNetwork,
664                                                   SocketType.Stream,
665                                                   ProtocolType.Tcp);
666                         
667                         sock.ExclusiveAddressUse = true;
668                         
669                         Assert.AreEqual (true, sock.ExclusiveAddressUse, "ExclusiveAddressUseUnbound");
670                         
671                         sock.Close ();
672                 }
673
674                 [Test]
675                 [ExpectedException (typeof(InvalidOperationException))]
676                 [Category ("NotWorking")] // Not supported on Linux
677                 public void ExclusiveAddressUseBound ()
678                 {
679                         Socket sock = new Socket (AddressFamily.InterNetwork,
680                                                   SocketType.Stream,
681                                                   ProtocolType.Tcp);
682                         
683                         sock.Bind (new IPEndPoint (IPAddress.Any, 1235));
684                         sock.ExclusiveAddressUse = true;
685                         sock.Close ();
686                 }
687
688                 [Test]
689                 [ExpectedException (typeof(ObjectDisposedException))]
690                 public void ExclusiveAddressUseClosed ()
691                 {
692                         Socket sock = new Socket (AddressFamily.InterNetwork,
693                                                   SocketType.Stream,
694                                                   ProtocolType.Tcp);
695                         
696                         sock.Close ();
697                         
698                         bool val = sock.ExclusiveAddressUse;
699                 }
700                 
701                 [Test]
702                 public void IsBoundTcp ()
703                 {
704                         Socket sock = new Socket (AddressFamily.InterNetwork,
705                                                   SocketType.Stream,
706                                                   ProtocolType.Tcp);
707                         IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
708                                                         BogusPort);
709                         
710                         Assert.AreEqual (false, sock.IsBound, "IsBoundTcp #1");
711                         
712                         sock.Bind (ep);
713                         Assert.AreEqual (true, sock.IsBound, "IsBoundTcp #2");
714
715                         sock.Listen (1);
716                         
717                         Socket sock2 = new Socket (AddressFamily.InterNetwork,
718                                                    SocketType.Stream,
719                                                    ProtocolType.Tcp);
720                         
721                         Assert.AreEqual (false, sock2.IsBound, "IsBoundTcp #3");
722                         
723                         sock2.Connect (ep);
724                         Assert.AreEqual (true, sock2.IsBound, "IsBoundTcp #4");
725                         
726                         sock2.Close ();
727                         Assert.AreEqual (true, sock2.IsBound, "IsBoundTcp #5");
728
729                         sock.Close ();
730                         Assert.AreEqual (true, sock.IsBound, "IsBoundTcp #6");
731                 }
732
733                 [Test]
734                 public void IsBoundUdp ()
735                 {
736                         Socket sock = new Socket (AddressFamily.InterNetwork,
737                                                   SocketType.Dgram,
738                                                   ProtocolType.Udp);
739                         IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
740                                                         BogusPort);
741                         
742                         Assert.AreEqual (false, sock.IsBound, "IsBoundUdp #1");
743                         
744                         sock.Bind (ep);
745                         Assert.AreEqual (true, sock.IsBound, "IsBoundUdp #2");
746                         
747                         sock.Close ();
748                         Assert.AreEqual (true, sock.IsBound, "IsBoundUdp #3");
749                         
750
751                         sock = new Socket (AddressFamily.InterNetwork,
752                                            SocketType.Dgram,
753                                            ProtocolType.Udp);
754                         
755                         Assert.AreEqual (false, sock.IsBound, "IsBoundUdp #4");
756                         
757                         sock.Connect (ep);
758                         Assert.AreEqual (true, sock.IsBound, "IsBoundUdp #5");
759                         
760                         sock.Close ();
761                         Assert.AreEqual (true, sock.IsBound, "IsBoundUdp #6");
762                 }
763
764                 [Test]
765                 /* Should not throw an exception */
766                 public void IsBoundClosed ()
767                 {
768                         Socket sock = new Socket (AddressFamily.InterNetwork,
769                                                   SocketType.Stream,
770                                                   ProtocolType.Tcp);
771                         
772                         sock.Close ();
773                         
774                         bool val = sock.IsBound;
775                 }
776                 
777                 /* Nothing much to test for LingerState */
778                 
779                 [Test]
780                 public void MulticastLoopbackDefaultTcp ()
781                 {
782                         Socket sock = new Socket (AddressFamily.InterNetwork,
783                                                   SocketType.Stream,
784                                                   ProtocolType.Tcp);
785                         
786                         try {
787                                 bool value = sock.MulticastLoopback;
788                                 Assert.Fail ("MulticastLoopbackDefaultTcp #1");
789                         } catch (SocketException ex) {
790                                 Assert.AreEqual (10042, ex.ErrorCode, "MulticastLoopbackDefaultTcp #2");
791                         } catch {
792                                 Assert.Fail ("MulticastLoopbackDefaultTcp #2");
793                         } finally {
794                                 sock.Close ();
795                         }
796                 }
797
798                 [Test]
799                 public void MulticastLoopbackChangeTcp ()
800                 {
801                         Socket sock = new Socket (AddressFamily.InterNetwork,
802                                                   SocketType.Stream,
803                                                   ProtocolType.Tcp);
804                         
805                         try {
806                                 sock.MulticastLoopback = false;
807                                 Assert.Fail ("MulticastLoopbackChangeTcp #1");
808                         } catch (SocketException ex) {
809                                 Assert.AreEqual (10042, ex.ErrorCode, "MulticastLoopbackChangeTcp #2");
810                         } catch {
811                                 Assert.Fail ("MulticastLoopbackChangeTcp #2");
812                         } finally {
813                                 sock.Close ();
814                         }
815                 }
816                 
817                 [Test]
818                 public void MulticastLoopbackDefaultUdp ()
819                 {
820                         Socket sock = new Socket (AddressFamily.InterNetwork,
821                                                   SocketType.Dgram,
822                                                   ProtocolType.Udp);
823                         
824                         Assert.AreEqual (true, sock.MulticastLoopback, "MulticastLoopbackDefaultUdp");
825                         
826                         sock.Close ();
827                 }
828                 
829                 [Test]
830                 public void MulticastLoopbackChangeUdp ()
831                 {
832                         Socket sock = new Socket (AddressFamily.InterNetwork,
833                                                   SocketType.Dgram,
834                                                   ProtocolType.Udp);
835                         
836                         sock.MulticastLoopback = false;
837                         
838                         Assert.AreEqual (false, sock.MulticastLoopback, "MulticastLoopbackChangeUdp");
839                         
840                         sock.Close ();
841                 }
842
843                 [Test]
844                 [ExpectedException (typeof(ObjectDisposedException))]
845                 public void MulticastLoopbackClosed ()
846                 {
847                         Socket sock = new Socket (AddressFamily.InterNetwork,
848                                                   SocketType.Stream,
849                                                   ProtocolType.Tcp);
850                         
851                         sock.Close ();
852                         
853                         bool val = sock.MulticastLoopback;
854                 }
855                 
856                 /* OSSupportsIPv6 depends on the environment */
857                 
858                 [Test]
859                 [Category("NotWorking")] // We have different defaults for perf reasons
860                 public void ReceiveBufferSizeDefault ()
861                 {
862                         Socket sock = new Socket (AddressFamily.InterNetwork,
863                                                   SocketType.Stream,
864                                                   ProtocolType.Tcp);
865                         
866                         Assert.AreEqual (8192, sock.ReceiveBufferSize, "ReceiveBufferSizeDefault");
867                         
868                         sock.Close ();
869                 }
870                 
871                 [Test]
872                 [Category("NotWorking")] // We have different defaults for perf reasons
873                 public void ReceiveBufferSizeDefaultUdp ()
874                 {
875                         Socket sock = new Socket (AddressFamily.InterNetwork,
876                                                   SocketType.Dgram,
877                                                   ProtocolType.Udp);
878                         
879                         Assert.AreEqual (8192, sock.ReceiveBufferSize, "ReceiveBufferSizeDefaultUdp");
880                         
881                         sock.Close ();
882                 }
883
884                 [Test]
885                 public void ReceiveBufferSizeChange ()
886                 {
887                         Socket sock = new Socket (AddressFamily.InterNetwork,
888                                                   SocketType.Stream,
889                                                   ProtocolType.Tcp);
890                         
891                         sock.ReceiveBufferSize = 16384;
892                         
893                         Assert.AreEqual (16384, sock.ReceiveBufferSize, "ReceiveBufferSizeChange");
894                         
895                         sock.Close ();
896                 }
897
898                 [Test]
899                 [Category("NotWorking")] // We cannot totally remove buffers (minimum is set to 256
900                 public void BuffersCheck_None ()
901                 {
902                         using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
903                                 int original = s.ReceiveBufferSize;
904                                 s.ReceiveBufferSize = 0;
905                                 Assert.AreEqual (0, s.ReceiveBufferSize, "ReceiveBufferSize " + original.ToString ());
906
907                                 original = s.SendBufferSize;
908                                 s.SendBufferSize = 0;
909                                 Assert.AreEqual (0, s.SendBufferSize, "SendBufferSize " + original.ToString ());
910                         }
911                 }
912
913                 [Test]
914                 [ExpectedException (typeof(ObjectDisposedException))]
915                 public void ReceiveBufferSizeClosed ()
916                 {
917                         Socket sock = new Socket (AddressFamily.InterNetwork,
918                                                   SocketType.Stream,
919                                                   ProtocolType.Tcp);
920                         
921                         sock.Close ();
922                         
923                         int val = sock.ReceiveBufferSize;
924                 }
925                 
926                 [Test]
927                 [Category("NotWorking")] // We have different defaults for perf reasons
928                 public void SendBufferSizeDefault ()
929                 {
930                         Socket sock = new Socket (AddressFamily.InterNetwork,
931                                                   SocketType.Stream,
932                                                   ProtocolType.Tcp);
933                         
934                         Assert.AreEqual (8192, sock.SendBufferSize, "SendBufferSizeDefault");
935                         
936                         sock.Close ();
937                 }
938                 
939                 [Test]
940                 [Category("NotWorking")] // We have different defaults for perf reasons
941                 public void SendBufferSizeDefaultUdp ()
942                 {
943                         Socket sock = new Socket (AddressFamily.InterNetwork,
944                                                   SocketType.Dgram,
945                                                   ProtocolType.Udp);
946                         
947                         Assert.AreEqual (8192, sock.SendBufferSize, "SendBufferSizeDefaultUdp");
948                         
949                         sock.Close ();
950                 }
951
952                 [Test]
953                 public void SendBufferSizeChange ()
954                 {
955                         Socket sock = new Socket (AddressFamily.InterNetwork,
956                                                   SocketType.Stream,
957                                                   ProtocolType.Tcp);
958                         
959                         sock.SendBufferSize = 16384;
960                         
961                         Assert.AreEqual (16384, sock.SendBufferSize, "SendBufferSizeChange");
962                         
963                         sock.Close ();
964                 }
965
966                 [Test]
967                 [ExpectedException (typeof(ObjectDisposedException))]
968                 public void SendBufferSizeClosed ()
969                 {
970                         Socket sock = new Socket (AddressFamily.InterNetwork,
971                                                   SocketType.Stream,
972                                                   ProtocolType.Tcp);
973                         
974                         sock.Close ();
975                         
976                         int val = sock.SendBufferSize;
977                 }
978                 
979                 /* No test for TTL default as it's platform dependent */
980                 [Test]
981                 public void TtlChange ()
982                 {
983                         Socket sock = new Socket (AddressFamily.InterNetwork,
984                                                   SocketType.Stream,
985                                                   ProtocolType.Tcp);
986                         
987                         sock.Ttl = 255;
988                         
989                         Assert.AreEqual (255, sock.Ttl, "TtlChange");
990                         
991                         sock.Close ();
992                 }
993
994                 [Test]
995                 [Category ("NotOnMac")] // Mac doesn't throw when overflowing the ttl
996                 public void TtlChangeOverflow ()
997                 {
998                         Socket sock = new Socket (AddressFamily.InterNetwork,
999                                                   SocketType.Stream,
1000                                                   ProtocolType.Tcp);
1001                         
1002                         try {
1003                                 sock.Ttl = 256;
1004                                 Assert.Fail ("TtlChangeOverflow #1");
1005                         } catch (SocketException ex) {
1006                                 Assert.AreEqual (10022, ex.ErrorCode,
1007                                                  "TtlChangeOverflow #2");
1008                         } catch {
1009                                 Assert.Fail ("TtlChangeoverflow #3");
1010                         } finally {
1011                                 sock.Close ();
1012                         }
1013                 }
1014                 
1015 /* Apparently you can set TTL=0 on the ms runtime!!
1016                         try {
1017                                 sock.Ttl = 0;
1018                                 Assert.Fail ("TtlChangeOverflow #4");
1019                         } catch (SocketException ex) {
1020                                 Assert.AreEqual (10022, ex.ErrorCode,
1021                                                  "TtlChangeOverflow #5");
1022                         } catch {
1023                                 Assert.Fail ("TtlChangeOverflow #6");
1024                         } finally {
1025                                 sock.Close ();
1026                         }
1027 */
1028
1029                 [Test]
1030                 [ExpectedException (typeof(ObjectDisposedException))]
1031                 public void TtlClosed ()
1032                 {
1033                         Socket sock = new Socket (AddressFamily.InterNetwork,
1034                                                   SocketType.Stream,
1035                                                   ProtocolType.Tcp);
1036                         
1037                         sock.Close ();
1038                         
1039                         int val = sock.Ttl;
1040                 }
1041                 
1042                 [Test]
1043                 public void UseOnlyOverlappedIODefault ()
1044                 {
1045                         Socket sock = new Socket (AddressFamily.InterNetwork,
1046                                                   SocketType.Stream,
1047                                                   ProtocolType.Tcp);
1048                         
1049                         Assert.AreEqual (false, sock.UseOnlyOverlappedIO, "UseOnlyOverlappedIODefault");
1050                         
1051                         sock.Close ();
1052                 }
1053
1054                 //
1055                 // We need this because the Linux kernel in certain configurations
1056                 // will end up rounding up the values passed on to the kernel
1057                 // for socket send/receive timeouts.
1058                 //
1059                 int Approximate (int target, int value)
1060                 {
1061                         int epsilon = 10;
1062                         
1063                         if (value > target-10 && value < target+10)
1064                                 return target;
1065                         return value;
1066                 }
1067                 
1068                 [Test]
1069                 public void UseOnlyOverlappedIOChange ()
1070                 {
1071                         Socket sock = new Socket (AddressFamily.InterNetwork,
1072                                                   SocketType.Stream,
1073                                                   ProtocolType.Tcp);
1074                         
1075                         sock.UseOnlyOverlappedIO = true;
1076                         
1077                         Assert.AreEqual (true, sock.UseOnlyOverlappedIO, "UseOnlyOverlappedIOChange");
1078                         
1079                         sock.Close ();
1080                 }
1081
1082                 [Test]
1083                 /* Should not throw an exception */
1084                 public void UseOnlyOverlappedIOClosed ()
1085                 {
1086                         Socket sock = new Socket (AddressFamily.InterNetwork,
1087                                                   SocketType.Stream,
1088                                                   ProtocolType.Tcp);
1089                         
1090                         sock.Close ();
1091                         
1092                         bool val = sock.UseOnlyOverlappedIO;
1093                 }
1094                 
1095                 [Test]
1096                 public void SendTimeoutDefault ()
1097                 {
1098                         Socket sock = new Socket (AddressFamily.InterNetwork,
1099                                                   SocketType.Stream,
1100                                                   ProtocolType.Tcp);
1101                         
1102                         Assert.AreEqual (0, sock.SendTimeout, "SendTimeoutDefault");
1103                         
1104                         sock.Close ();
1105                 }
1106
1107                 [Test]
1108                 public void SendTimeoutChange ()
1109                 {
1110                         Socket sock = new Socket (AddressFamily.InterNetwork,
1111                                                   SocketType.Stream,
1112                                                   ProtocolType.Tcp);
1113                         
1114                         /* Should be rounded up to 500, according to
1115                          * the MSDN docs, but the MS runtime doesn't
1116                          */
1117                         sock.SendTimeout = 50;
1118                         Assert.AreEqual (50, Approximate (50, sock.SendTimeout), "SendTimeoutChange #1");
1119                         
1120                         sock.SendTimeout = 2000;
1121                         Assert.AreEqual (2000, Approximate (2000, sock.SendTimeout), "SendTimeoutChange #2");
1122                         
1123                         sock.SendTimeout = 0;
1124                         Assert.AreEqual (0, Approximate (0, sock.SendTimeout), "SendTimeoutChange #3");
1125                         
1126                         /* Should be the same as setting 0 */
1127                         sock.SendTimeout = -1;
1128                         Assert.AreEqual (0, sock.SendTimeout, "SendTimeoutChange #4");
1129
1130                         sock.SendTimeout = 65536;
1131                         Assert.AreEqual (65536, Approximate (65536, sock.SendTimeout), "SendTimeoutChange #5");
1132                         
1133                         try {
1134                                 sock.SendTimeout = -2;
1135                                 Assert.Fail ("SendTimeoutChange #8");
1136                         } catch (ArgumentOutOfRangeException) {
1137                         } catch {
1138                                 Assert.Fail ("SendTimeoutChange #9");
1139                         } finally {
1140                                 sock.Close ();
1141                         }
1142                 }
1143
1144                 [Test]
1145                 [ExpectedException (typeof(ObjectDisposedException))]
1146                 public void SendTimeoutClosed ()
1147                 {
1148                         Socket sock = new Socket (AddressFamily.InterNetwork,
1149                                                   SocketType.Stream,
1150                                                   ProtocolType.Tcp);
1151                         
1152                         sock.Close ();
1153                         
1154                         int val = sock.SendTimeout;
1155                 }
1156                 
1157                 [Test]
1158                 public void ReceiveTimeoutDefault ()
1159                 {
1160                         Socket sock = new Socket (AddressFamily.InterNetwork,
1161                                                   SocketType.Stream,
1162                                                   ProtocolType.Tcp);
1163                         
1164                         Assert.AreEqual (0, sock.ReceiveTimeout, "ReceiveTimeoutDefault");
1165                         
1166                         sock.Close ();
1167                 }
1168
1169                 [Test]
1170                 public void ReceiveTimeoutChange ()
1171                 {
1172                         Socket sock = new Socket (AddressFamily.InterNetwork,
1173                                                   SocketType.Stream,
1174                                                   ProtocolType.Tcp);
1175                         
1176                         sock.ReceiveTimeout = 50;
1177                         Assert.AreEqual (50, Approximate (50, sock.ReceiveTimeout), "ReceiveTimeoutChange #1");
1178                         
1179                         sock.ReceiveTimeout = 2000;
1180                         Assert.AreEqual (2000, Approximate (2000, sock.ReceiveTimeout), "ReceiveTimeoutChange #2");
1181                         
1182                         sock.ReceiveTimeout = 0;
1183                         Assert.AreEqual (0, sock.ReceiveTimeout, "ReceiveTimeoutChange #3");
1184                         
1185                         /* Should be the same as setting 0 */
1186                         sock.ReceiveTimeout = -1;
1187                         Assert.AreEqual (0, sock.ReceiveTimeout, "ReceiveTimeoutChange #4");
1188
1189                         sock.ReceiveTimeout = 65536;
1190                         Assert.AreEqual (65536, Approximate (65536, sock.ReceiveTimeout), "ReceiveTimeoutChange #5");
1191                         
1192                         try {
1193                                 sock.ReceiveTimeout = -2;
1194                                 Assert.Fail ("ReceiveTimeoutChange #8");
1195                         } catch (ArgumentOutOfRangeException) {
1196                         } catch {
1197                                 Assert.Fail ("ReceiveTimeoutChange #9");
1198                         } finally {
1199                                 sock.Close ();
1200                         }
1201                 }
1202
1203                 [Test]
1204                 [ExpectedException (typeof(ObjectDisposedException))]
1205                 public void ReceiveTimeoutClosed ()
1206                 {
1207                         Socket sock = new Socket (AddressFamily.InterNetwork,
1208                                                   SocketType.Stream,
1209                                                   ProtocolType.Tcp);
1210                         
1211                         sock.Close ();
1212                         
1213                         int val = sock.ReceiveTimeout;
1214                 }
1215                 
1216                 [Test]
1217                 public void NoDelayDefaultTcp ()
1218                 {
1219                         Socket sock = new Socket (AddressFamily.InterNetwork,
1220                                                   SocketType.Stream,
1221                                                   ProtocolType.Tcp);
1222                         
1223                         Assert.AreEqual (false, sock.NoDelay, "NoDelayDefaultTcp");
1224                         
1225                         sock.Close ();
1226                 }
1227
1228                 [Test]
1229                 public void NoDelayChangeTcp ()
1230                 {
1231                         Socket sock = new Socket (AddressFamily.InterNetwork,
1232                                                   SocketType.Stream,
1233                                                   ProtocolType.Tcp);
1234                         
1235                         sock.NoDelay = true;
1236                         
1237                         Assert.AreEqual (true, sock.NoDelay, "NoDelayChangeTcp");
1238                         
1239                         sock.Close ();
1240                 }
1241                 
1242                 [Test]
1243                 public void NoDelayDefaultUdp ()
1244                 {
1245                         Socket sock = new Socket (AddressFamily.InterNetwork,
1246                                                   SocketType.Dgram,
1247                                                   ProtocolType.Udp);
1248                         
1249                         try {
1250                                 bool val = sock.NoDelay;
1251                                 Assert.Fail ("NoDelayDefaultUdp #1");
1252                         } catch (SocketException ex) {
1253                                 Assert.AreEqual (10042, ex.ErrorCode,
1254                                                  "NoDelayDefaultUdp #2");
1255                         } catch {
1256                                 Assert.Fail ("NoDelayDefaultUdp #3");
1257                         } finally {
1258                                 sock.Close ();
1259                         }
1260                 }
1261
1262                 [Test]
1263                 public void NoDelayChangeUdp ()
1264                 {
1265                         Socket sock = new Socket (AddressFamily.InterNetwork,
1266                                                   SocketType.Dgram,
1267                                                   ProtocolType.Udp);
1268                         
1269                         try {
1270                                 sock.NoDelay = true;
1271                                 Assert.Fail ("NoDelayChangeUdp #1");
1272                         } catch (SocketException ex) {
1273                                 Assert.AreEqual (10042, ex.ErrorCode,
1274                                                  "NoDelayChangeUdp #2");
1275                         } catch {
1276                                 Assert.Fail ("NoDelayChangeUdp #3");
1277                         } finally {
1278                                 sock.Close ();
1279                         }
1280                 }
1281                 
1282                 [Test]
1283                 [ExpectedException (typeof(ObjectDisposedException))]
1284                 public void NoDelayClosed ()
1285                 {
1286                         Socket sock = new Socket (AddressFamily.InterNetwork,
1287                                                   SocketType.Stream,
1288                                                   ProtocolType.Tcp);
1289                         
1290                         sock.Close ();
1291                         
1292                         bool val = sock.NoDelay;
1293                 }
1294
1295                 static bool BAAccepted = false;
1296                 static Socket BASocket = null;
1297                 static ManualResetEvent BACalledBack = new ManualResetEvent (false);
1298                 
1299                 private static void BACallback (IAsyncResult asyncResult)
1300                 {
1301                         Socket sock = (Socket)asyncResult.AsyncState;
1302                         
1303                         BASocket = sock.EndAccept (asyncResult);
1304                         
1305                         BAAccepted = true;
1306                         BACalledBack.Set ();
1307                 }
1308                 
1309                 [Test]
1310                 [ExpectedException (typeof(InvalidOperationException))]
1311                 public void BeginAcceptNotBound ()
1312                 {
1313                         Socket sock = new Socket (AddressFamily.InterNetwork,
1314                                                   SocketType.Stream,
1315                                                   ProtocolType.Tcp);
1316
1317                         sock.BeginAccept (BACallback, sock);
1318                         
1319                         sock.Close ();
1320                 }
1321                 
1322                 [Test]
1323                 [ExpectedException (typeof(InvalidOperationException))]
1324                 public void BeginAcceptNotListening ()
1325                 {
1326                         Socket sock = new Socket (AddressFamily.InterNetwork,
1327                                                   SocketType.Stream,
1328                                                   ProtocolType.Tcp);
1329
1330                         sock.Bind (new IPEndPoint (IPAddress.Any, 1236));
1331                         
1332                         sock.BeginAccept (BACallback, sock);
1333                         
1334                         sock.Close ();
1335                 }
1336
1337                 [Test]
1338                 public void BeginAccept ()
1339                 {
1340                         Socket sock = new Socket (AddressFamily.InterNetwork,
1341                                                   SocketType.Stream,
1342                                                   ProtocolType.Tcp);
1343                         IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
1344                                                         1237);
1345                         
1346                         sock.Bind (ep);
1347                         sock.Listen (1);
1348                         
1349                         BACalledBack.Reset ();
1350                         
1351                         sock.BeginAccept (BACallback, sock);
1352
1353                         Socket conn = new Socket (AddressFamily.InterNetwork,
1354                                                   SocketType.Stream,
1355                                                   ProtocolType.Tcp);
1356                         
1357                         conn.Connect (ep);
1358
1359                         if (BACalledBack.WaitOne (2000, false) == false) {
1360                                 Assert.Fail ("BeginAccept wait timed out");
1361                         }
1362                         
1363                         Assert.AreEqual (true, BAAccepted, "BeginAccept #1");
1364                         Assert.AreEqual (true, BASocket.Connected, "BeginAccept #2");
1365                         Assert.AreEqual (false, sock.Connected, "BeginAccept #3");
1366                         Assert.AreEqual (true, conn.Connected, "BeginAccept #4");
1367
1368                         BASocket.Close ();
1369                         conn.Close ();
1370                         sock.Close ();
1371                 }
1372
1373                 [Test]
1374                 [ExpectedException (typeof(ObjectDisposedException))]
1375                 public void BeginAcceptClosed ()
1376                 {
1377                         Socket sock = new Socket (AddressFamily.InterNetwork,
1378                                                   SocketType.Stream,
1379                                                   ProtocolType.Tcp);
1380                         
1381                         sock.Close ();
1382                         
1383                         sock.BeginAccept (BACallback, sock);
1384                 }
1385
1386                 static bool BADAccepted = false;
1387                 static Socket BADSocket = null;
1388                 static byte[] BADBytes;
1389                 static int BADByteCount;
1390                 static ManualResetEvent BADCalledBack = new ManualResetEvent (false);
1391                 
1392                 private static void BADCallback (IAsyncResult asyncResult)
1393                 {
1394                         Socket sock = (Socket)asyncResult.AsyncState;
1395                         
1396                         BADSocket = sock.EndAccept (out BADBytes,
1397                                                     out BADByteCount,
1398                                                     asyncResult);
1399                         
1400                         BADAccepted = true;
1401                         BADCalledBack.Set ();
1402                 }
1403
1404                 [Test]
1405                 public void BeginAcceptData ()
1406                 {
1407                         Socket sock = new Socket (AddressFamily.InterNetwork,
1408                                                   SocketType.Stream,
1409                                                   ProtocolType.Tcp);
1410                         IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
1411                                                         1238);
1412                         
1413                         sock.Bind (ep);
1414                         sock.Listen (1);
1415                         
1416                         BADCalledBack.Reset ();
1417                         
1418                         sock.BeginAccept (256, BADCallback, sock);
1419
1420                         Socket conn = new Socket (AddressFamily.InterNetwork,
1421                                                   SocketType.Stream,
1422                                                   ProtocolType.Tcp);
1423                         byte[] send_bytes = new byte[] {10, 11, 12, 13};
1424                         
1425                         conn.Connect (ep);
1426                         conn.Send (send_bytes);
1427
1428                         if (BADCalledBack.WaitOne (2000, false) == false) {
1429                                 Assert.Fail ("BeginAcceptData wait timed out");
1430                         }
1431                         
1432                         Assert.AreEqual (true, BADAccepted, "BeginAcceptData #1");
1433                         Assert.AreEqual (true, BADSocket.Connected, "BeginAcceptData #2");
1434                         Assert.AreEqual (false, sock.Connected, "BeginAcceptData #3");
1435                         Assert.AreEqual (true, conn.Connected, "BeginAcceptData #4");
1436                         Assert.AreEqual (send_bytes.Length, BADByteCount, "BeginAcceptData #5");
1437                         
1438                         /* The MS runtime gives the returned data in a
1439                          * much bigger array.  TODO: investigate
1440                          * whether it the size correlates to the first
1441                          * parameter in BeginAccept()
1442                          */
1443                         Assert.IsFalse (BADBytes.Length == send_bytes.Length,
1444                                         "BeginAcceptData #6");
1445
1446                         for(int i = 0; i < send_bytes.Length; i++) {
1447                                 Assert.AreEqual (send_bytes[i], BADBytes[i], "BeginAcceptData #" + (i+7).ToString ());
1448                         }
1449
1450                         BADSocket.Close ();
1451                         conn.Close ();
1452                         sock.Close ();
1453                 }
1454
1455                 [Test]
1456                 [ExpectedException (typeof(ObjectDisposedException))]
1457                 public void BeginAcceptDataClosed ()
1458                 {
1459                         Socket sock = new Socket (AddressFamily.InterNetwork,
1460                                                   SocketType.Stream,
1461                                                   ProtocolType.Tcp);
1462                         
1463                         sock.Close ();
1464                         
1465                         sock.BeginAccept (256, BADCallback, sock);
1466                 }
1467
1468                 [Test]
1469                 public void BeginAcceptSocketUdp ()
1470                 {
1471                         Socket sock = new Socket (AddressFamily.InterNetwork,
1472                                                   SocketType.Stream,
1473                                                   ProtocolType.Tcp);
1474                         Socket acc = new Socket (AddressFamily.InterNetwork,
1475                                                  SocketType.Dgram,
1476                                                  ProtocolType.Udp);
1477                         
1478                         IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
1479                                                         1239);
1480                         
1481                         sock.Bind (ep);
1482                         sock.Listen (1);
1483                         
1484                         try {
1485                                 sock.BeginAccept (acc, 256, BADCallback, sock);
1486                                 Assert.Fail ("BeginAcceptSocketUdp #1");
1487                         } catch (SocketException ex) {
1488                                 Assert.AreEqual (10022, ex.ErrorCode, "BeginAcceptSocketUdp #2");
1489                         } catch {
1490                                 Assert.Fail ("BeginAcceptSocketUdp #3");
1491                         } finally {
1492                                 acc.Close ();
1493                                 sock.Close ();
1494                         }
1495                 }
1496                 
1497                 [Test]
1498                 public void BeginAcceptSocketBound ()
1499                 {
1500                         Socket sock = new Socket (AddressFamily.InterNetwork,
1501                                                   SocketType.Stream,
1502                                                   ProtocolType.Tcp);
1503                         Socket acc = new Socket (AddressFamily.InterNetwork,
1504                                                  SocketType.Stream,
1505                                                  ProtocolType.Tcp);
1506                         
1507                         IPEndPoint ep1 = new IPEndPoint (IPAddress.Loopback,
1508                                                          1240);
1509                         
1510                         IPEndPoint ep2 = new IPEndPoint (IPAddress.Loopback,
1511                                                          1241);
1512                         
1513                         sock.Bind (ep1);
1514                         sock.Listen (1);
1515
1516                         acc.Bind (ep2);
1517                         
1518                         try {
1519                                 sock.BeginAccept (acc, 256, BADCallback, sock);
1520                                 Assert.Fail ("BeginAcceptSocketBound #1");
1521                         } catch (InvalidOperationException) {
1522                         } catch {
1523                                 Assert.Fail ("BeginAcceptSocketBound #2");
1524                         } finally {
1525                                 acc.Close ();
1526                                 sock.Close ();
1527                         }
1528                 }
1529                 
1530                 [Test]
1531                 public void BeginAcceptSocket ()
1532                 {
1533                         Socket sock = new Socket (AddressFamily.InterNetwork,
1534                                                   SocketType.Stream,
1535                                                   ProtocolType.Tcp);
1536                         Socket acc = new Socket (AddressFamily.InterNetwork,
1537                                                  SocketType.Stream,
1538                                                  ProtocolType.Tcp);
1539                         
1540                         IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
1541                                                         1242);
1542                         
1543                         sock.Bind (ep);
1544                         sock.Listen (1);
1545                         
1546                         BADCalledBack.Reset ();
1547                         
1548                         sock.BeginAccept (acc, 256, BADCallback, sock);
1549
1550                         Socket conn = new Socket (AddressFamily.InterNetwork,
1551                                                   SocketType.Stream,
1552                                                   ProtocolType.Tcp);
1553                         byte[] send_bytes = new byte[] {10, 11, 12, 13};
1554                         
1555                         conn.Connect (ep);
1556                         conn.Send (send_bytes);
1557
1558                         if (BADCalledBack.WaitOne (2000, false) == false) {
1559                                 Assert.Fail ("BeginAcceptSocket wait timed out");
1560                         }
1561                         
1562                         Assert.AreEqual (true, BADAccepted, "BeginAcceptSocket #1");
1563                         Assert.AreEqual (true, BADSocket.Connected, "BeginAcceptSocket #2");
1564                         Assert.AreEqual (false, sock.Connected, "BeginAcceptSocket #3");
1565                         Assert.AreEqual (true, conn.Connected, "BeginAcceptSocket #4");
1566                         Assert.AreEqual (send_bytes.Length, BADByteCount, "BeginAcceptSocket #5");
1567                         Assert.AreEqual (AddressFamily.InterNetwork, acc.AddressFamily, "BeginAcceptSocket #6");
1568                         Assert.AreEqual (SocketType.Stream, acc.SocketType, "BeginAcceptSocket #7");
1569                         Assert.AreEqual (ProtocolType.Tcp, acc.ProtocolType, "BeginAcceptSocket #8");
1570                         Assert.AreEqual (conn.LocalEndPoint, acc.RemoteEndPoint, "BeginAcceptSocket #9");
1571                         
1572                         /* The MS runtime gives the returned data in a
1573                          * much bigger array.  TODO: investigate
1574                          * whether it the size correlates to the first
1575                          * parameter in BeginAccept()
1576                          */
1577                         Assert.IsFalse (BADBytes.Length == send_bytes.Length,
1578                                         "BeginAcceptSocket #10");
1579
1580                         for(int i = 0; i < send_bytes.Length; i++) {
1581                                 Assert.AreEqual (send_bytes[i], BADBytes[i], "BeginAcceptSocket #" + (i+11).ToString ());
1582                         }
1583
1584                         BADSocket.Close ();
1585                         conn.Close ();
1586                         acc.Close ();
1587                         sock.Close ();
1588                 }
1589
1590                 [Test]
1591                 public void BeginAcceptSocketClosed ()
1592                 {
1593                         Socket sock = new Socket (AddressFamily.InterNetwork,
1594                                                   SocketType.Stream,
1595                                                   ProtocolType.Tcp);
1596                         Socket acc = new Socket (AddressFamily.InterNetwork,
1597                                                  SocketType.Stream,
1598                                                  ProtocolType.Tcp);
1599                         
1600                         sock.Close ();
1601                         
1602                         try {
1603                                 sock.BeginAccept (acc, 256, BADCallback, null);
1604                                 Assert.Fail ("BeginAcceptSocketClosed #1");
1605                         } catch (ObjectDisposedException) {
1606                         } catch {
1607                                 Assert.Fail ("BeginAcceptSocketClosed #2");
1608                         } finally {
1609                                 acc.Close ();
1610                         }
1611                 }
1612
1613                 [Test]
1614                 public void BeginAcceptSocketAccClosed ()
1615                 {
1616                         Socket sock = new Socket (AddressFamily.InterNetwork,
1617                                                   SocketType.Stream,
1618                                                   ProtocolType.Tcp);
1619                         Socket acc = new Socket (AddressFamily.InterNetwork,
1620                                                  SocketType.Stream,
1621                                                  ProtocolType.Tcp);
1622                         IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
1623                                                         1243);
1624
1625                         sock.Bind (ep);
1626                         sock.Listen (1);
1627                         
1628                         acc.Close ();
1629                         
1630                         BADCalledBack.Reset ();
1631                         
1632                         try {
1633                                 sock.BeginAccept (acc, 256, BADCallback, null);
1634                                 Assert.Fail ("BeginAcceptSocketAccClosed #1");
1635                         } catch (ObjectDisposedException) {
1636                         } catch {
1637                                 Assert.Fail ("BeginAcceptSocketAccClosed #2");
1638                         } finally {
1639                                 sock.Close ();
1640                         }
1641                 }
1642                 
1643                 static bool BCConnected = false;
1644                 static ManualResetEvent BCCalledBack = new ManualResetEvent (false);
1645                 
1646                 private static void BCCallback (IAsyncResult asyncResult)
1647                 {
1648                         Socket sock = (Socket)asyncResult.AsyncState;
1649                         
1650                         sock.EndConnect (asyncResult);
1651                         BCConnected = true;
1652                         
1653                         BCCalledBack.Set ();
1654                 }
1655                 
1656                 [Test]
1657                 public void BeginConnectAddressPort ()
1658                 {
1659                         Socket sock = new Socket (AddressFamily.InterNetwork,
1660                                                   SocketType.Stream,
1661                                                   ProtocolType.Tcp);
1662                         Socket listen = new Socket (AddressFamily.InterNetwork,
1663                                                     SocketType.Stream,
1664                                                     ProtocolType.Tcp);
1665                         IPAddress ip = IPAddress.Loopback;
1666                         IPEndPoint ep = new IPEndPoint (ip, 1244);
1667
1668                         listen.Bind (ep);
1669                         listen.Listen (1);
1670                         
1671                         BCCalledBack.Reset ();
1672                         
1673                         BCConnected = false;
1674                         
1675                         sock.BeginConnect (ip, 1244, BCCallback, sock);
1676
1677                         if (BCCalledBack.WaitOne (2000, false) == false) {
1678                                 Assert.Fail ("BeginConnectAddressPort wait timed out");
1679                         }
1680                         
1681                         Assert.AreEqual (true, BCConnected, "BeginConnectAddressPort #1");
1682                         
1683                         sock.Close ();
1684                         listen.Close ();
1685                 }
1686
1687                 [Test]
1688                 public void BeginConnectAddressPortNull ()
1689                 {
1690                         Socket sock = new Socket (AddressFamily.InterNetwork,
1691                                                   SocketType.Stream,
1692                                                   ProtocolType.Tcp);
1693                         IPAddress ip = null;
1694
1695                         try {
1696                                 sock.BeginConnect (ip, 1244, BCCallback,
1697                                                    sock);
1698                                 Assert.Fail ("BeginConnectAddressPortNull #1");
1699                         } catch (ArgumentNullException) {
1700                         } catch {
1701                                 Assert.Fail ("BeginConnectAddressPortNull #2");
1702                         } finally {
1703                                 sock.Close ();
1704                         }
1705                 }
1706
1707                 [Test]
1708                 public void BeginConnectAddressPortListen ()
1709                 {
1710                         Socket sock = new Socket (AddressFamily.InterNetwork,
1711                                                   SocketType.Stream,
1712                                                   ProtocolType.Tcp);
1713                         IPAddress ip = IPAddress.Loopback;
1714                         IPEndPoint ep = new IPEndPoint (ip, 1245);
1715
1716                         sock.Bind (ep);
1717                         sock.Listen (1);
1718                         
1719                         try {
1720                                 sock.BeginConnect (ip, 1245, BCCallback, sock);
1721                                 Assert.Fail ("BeginConnectAddressPortListen #1");
1722                         } catch (InvalidOperationException) {
1723                         } catch {
1724                                 Assert.Fail ("BeginConnectAddressPortListen #2");
1725                         } finally {
1726                                 sock.Close ();
1727                         }
1728                 }
1729                 
1730                 [Test]
1731                 [ExpectedException (typeof(ObjectDisposedException))]
1732                 public void BeginConnectAddressPortClosed ()
1733                 {
1734                         Socket sock = new Socket (AddressFamily.InterNetwork,
1735                                                   SocketType.Stream,
1736                                                   ProtocolType.Tcp);
1737                         IPAddress ip = IPAddress.Loopback;
1738                         
1739                         sock.Close ();
1740                         
1741                         sock.BeginConnect (ip, 1244, BCCallback, sock);
1742                 }
1743                 
1744                 [Test]
1745                 [Category ("NotOnMac")]
1746                 /*
1747                  * This is not a Mono bug.
1748                  * 
1749                  * By default, only 127.0.0.1 is enabled and you must explicitly
1750                  * enable additional addresses using 'sudo ifconfig lo0 alias 127.0.0.1'.
1751                  * 
1752                  * I tested this on Mac OS 10.7.4; a 'ping 127.0.0.2' does not work
1753                  * until I add that alias.
1754                  * 
1755                  */
1756                 public void BeginConnectMultiple ()
1757                 {
1758                         Socket sock = new Socket (AddressFamily.InterNetwork,
1759                                                   SocketType.Stream,
1760                                                   ProtocolType.Tcp);
1761                         Socket listen = new Socket (AddressFamily.InterNetwork,
1762                                                     SocketType.Stream,
1763                                                     ProtocolType.Tcp);
1764                         IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
1765                                                         1246);
1766                         IPAddress[] ips = new IPAddress[4];
1767                         
1768                         ips[0] = IPAddress.Parse ("127.0.0.4");
1769                         ips[1] = IPAddress.Parse ("127.0.0.3");
1770                         ips[2] = IPAddress.Parse ("127.0.0.2");
1771                         ips[3] = IPAddress.Parse ("127.0.0.1");
1772
1773                         listen.Bind (ep);
1774                         listen.Listen (1);
1775                         
1776                         BCCalledBack.Reset ();
1777                         
1778                         BCConnected = false;
1779                         
1780                         sock.BeginConnect (ips, 1246, BCCallback, sock);
1781                         
1782                         /* Longer wait here, because the ms runtime
1783                          * takes a lot longer to not connect
1784                          */
1785                         if (BCCalledBack.WaitOne (10000, false) == false) {
1786                                 Assert.Fail ("BeginConnectMultiple wait failed");
1787                         }
1788                         
1789                         Assert.AreEqual (true, BCConnected, "BeginConnectMultiple #1");
1790                         Assert.AreEqual (AddressFamily.InterNetwork, sock.RemoteEndPoint.AddressFamily, "BeginConnectMultiple #2");
1791                         IPEndPoint remep = (IPEndPoint)sock.RemoteEndPoint;
1792                         
1793                         Assert.AreEqual (IPAddress.Loopback, remep.Address, "BeginConnectMultiple #2");
1794                         
1795                         sock.Close ();
1796                         listen.Close ();
1797                 }
1798
1799                 [Test]
1800                 public void BeginConnectMultiple2 ()
1801                 {
1802                         Socket sock = new Socket (AddressFamily.InterNetwork,
1803                                                   SocketType.Stream,
1804                                                   ProtocolType.Tcp);
1805                         Socket listen = new Socket (AddressFamily.InterNetwork,
1806                                                     SocketType.Stream,
1807                                                     ProtocolType.Tcp);
1808
1809                         // Need at least two addresses.
1810                         var ips = Dns.GetHostAddresses (string.Empty);
1811                         if (ips.Length < 1)
1812                                 return;
1813
1814                         var allIps = new IPAddress [ips.Length + 1];
1815                         allIps [0] = IPAddress.Loopback;
1816                         ips.CopyTo (allIps, 1);
1817
1818                         /*
1819                          * Only bind to the loopback interface, so all the non-loopback
1820                          * IP addresses will fail.  BeginConnect()/EndConnect() should
1821                          * succeed it it can connect to at least one of the requested
1822                          * addresses.
1823                          */
1824                         IPEndPoint ep = new IPEndPoint (IPAddress.Loopback, 1246);
1825
1826                         listen.Bind (ep);
1827                         listen.Listen (1);
1828                         
1829                         BCCalledBack.Reset ();
1830                         
1831                         BCConnected = false;
1832                         
1833                         sock.BeginConnect (allIps, 1246, BCCallback, sock);
1834                         
1835                         /* Longer wait here, because the ms runtime
1836                          * takes a lot longer to not connect
1837                          */
1838                         if (BCCalledBack.WaitOne (10000, false) == false) {
1839                                 Assert.Fail ("BeginConnectMultiple2 wait failed");
1840                         }
1841                         
1842                         Assert.AreEqual (true, BCConnected, "BeginConnectMultiple2 #1");
1843                         Assert.AreEqual (AddressFamily.InterNetwork, sock.RemoteEndPoint.AddressFamily, "BeginConnectMultiple2 #2");
1844                         IPEndPoint remep = (IPEndPoint)sock.RemoteEndPoint;
1845
1846                         Assert.AreEqual (IPAddress.Loopback, remep.Address, "BeginConnectMultiple2 #2");
1847
1848                         sock.Close ();
1849                         listen.Close ();
1850                 }
1851
1852
1853                 [Test]
1854                 public void BeginConnectMultipleNull ()
1855                 {
1856                         Socket sock = new Socket (AddressFamily.InterNetwork,
1857                                                   SocketType.Stream,
1858                                                   ProtocolType.Tcp);
1859                         IPAddress[] ips = null;
1860                         
1861                         try {
1862                                 sock.BeginConnect (ips, 1246, BCCallback,
1863                                                    sock);
1864                                 Assert.Fail ("BeginConnectMultipleNull #1");
1865                         } catch (ArgumentNullException) {
1866                         } catch {
1867                                 Assert.Fail ("BeginConnectMultipleNull #2");
1868                         } finally {
1869                                 sock.Close ();
1870                         }
1871                 }
1872
1873                 [Test]
1874                 public void BeginConnectMultipleListen ()
1875                 {
1876                         Socket sock = new Socket (AddressFamily.InterNetwork,
1877                                                   SocketType.Stream,
1878                                                   ProtocolType.Tcp);
1879                         IPAddress[] ips = new IPAddress[4];
1880                         IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
1881                                                         1247);
1882                         
1883                         ips[0] = IPAddress.Parse ("127.0.0.4");
1884                         ips[1] = IPAddress.Parse ("127.0.0.3");
1885                         ips[2] = IPAddress.Parse ("127.0.0.2");
1886                         ips[3] = IPAddress.Parse ("127.0.0.1");
1887                         
1888                         sock.Bind (ep);
1889                         sock.Listen (1);
1890                         
1891                         try {
1892                                 sock.BeginConnect (ips, 1247, BCCallback,
1893                                                    sock);
1894                                 Assert.Fail ("BeginConnectMultipleListen #1");
1895                         } catch (InvalidOperationException) {
1896                         } catch {
1897                                 Assert.Fail ("BeginConnectMultipleListen #2");
1898                         } finally {
1899                                 sock.Close ();
1900                         }
1901                 }
1902                 
1903                 [Test]
1904                 [ExpectedException (typeof(ObjectDisposedException))]
1905                 public void BeginConnectMultipleClosed ()
1906                 {
1907                         Socket sock = new Socket (AddressFamily.InterNetwork,
1908                                                   SocketType.Stream,
1909                                                   ProtocolType.Tcp);
1910                         IPAddress[] ips = new IPAddress[4];
1911                         
1912                         ips[0] = IPAddress.Parse ("127.0.0.4");
1913                         ips[1] = IPAddress.Parse ("127.0.0.3");
1914                         ips[2] = IPAddress.Parse ("127.0.0.2");
1915                         ips[3] = IPAddress.Parse ("127.0.0.1");
1916                         
1917                         sock.Close ();
1918                         
1919                         sock.BeginConnect (ips, 1247, BCCallback, sock);
1920                 }
1921                 
1922                 [Test]
1923                 public void BeginConnectHostPortNull ()
1924                 {
1925                         Socket sock = new Socket (AddressFamily.InterNetwork,
1926                                                   SocketType.Stream,
1927                                                   ProtocolType.Tcp);
1928                         
1929                         try {
1930                                 sock.BeginConnect ((string)null, 0,
1931                                                    BCCallback, sock);
1932                                 Assert.Fail ("BeginConnectHostPort #1");
1933                         } catch (ArgumentNullException) {
1934                         } catch {
1935                                 Assert.Fail ("BeginConnectHostPort #2");
1936                         } finally {
1937                                 sock.Close ();
1938                         }
1939                 }
1940
1941                 [Test]
1942                 public void BeginConnectHostPortListen ()
1943                 {
1944                         Socket sock = new Socket (AddressFamily.InterNetwork,
1945                                                   SocketType.Stream,
1946                                                   ProtocolType.Tcp);
1947                         IPAddress ip = IPAddress.Loopback;
1948                         IPEndPoint ep = new IPEndPoint (ip, 1248);
1949                         
1950                         sock.Bind (ep);
1951                         sock.Listen (1);
1952                         
1953                         try {
1954                                 sock.BeginConnect ("localhost", 1248,
1955                                                    BCCallback, sock);
1956                                 Assert.Fail ("BeginConnectHostPortListen #1");
1957                         } catch (InvalidOperationException) {
1958                         } catch {
1959                                 Assert.Fail ("BeginConnectHostPortListen #2");
1960                         } finally {
1961                                 sock.Close ();
1962                         }
1963                 }
1964
1965                 [Test]
1966                 [Category ("NotWorking")] // Need to pick a non-IP AddressFamily that "works" on both mono and ms, this one only works on ms
1967                 public void BeginConnectHostPortNotIP ()
1968                 {
1969                         Socket sock = new Socket (AddressFamily.NetBios,
1970                                                   SocketType.Seqpacket,
1971                                                   ProtocolType.Unspecified);
1972                         
1973                         try {
1974                                 sock.BeginConnect ("localhost", 0, BCCallback,
1975                                                    sock);
1976                                 Assert.Fail ("BeginConnectHostPortNotIP #1");
1977                         } catch (NotSupportedException) {
1978                         } catch {
1979                                 Assert.Fail ("BeginConnectHostPortNotIP #2");
1980                         } finally {
1981                                 sock.Close ();
1982                         }
1983                 }
1984
1985                 [Test]
1986                 [ExpectedException (typeof(ObjectDisposedException))]
1987                 public void BeginConnectHostPortClosed ()
1988                 {
1989                         Socket sock = new Socket (AddressFamily.InterNetwork,
1990                                                   SocketType.Stream,
1991                                                   ProtocolType.Tcp);
1992                         
1993                         sock.Close ();
1994                         
1995                         sock.BeginConnect ("localhost", 0, BCCallback, sock);
1996                 }
1997                 
1998                 static bool BDDisconnected = false;
1999                 static ManualResetEvent BDCalledBack = new ManualResetEvent (false);
2000                 
2001                 private static void BDCallback (IAsyncResult asyncResult)
2002                 {
2003                         Socket sock = (Socket)asyncResult.AsyncState;
2004                         
2005                         sock.EndDisconnect (asyncResult);
2006                         BDDisconnected = true;
2007                         
2008                         BDCalledBack.Set ();
2009                 }
2010                 
2011                 [Test]
2012                 [Category ("NotDotNet")] // "Needs XP or later"
2013                 public void BeginDisconnect ()
2014                 {
2015                         Socket sock = new Socket (AddressFamily.InterNetwork,
2016                                                   SocketType.Stream,
2017                                                   ProtocolType.Tcp);
2018                         Socket listen = new Socket (AddressFamily.InterNetwork,
2019                                                     SocketType.Stream,
2020                                                     ProtocolType.Tcp);
2021                         IPAddress ip = IPAddress.Loopback;
2022                         IPEndPoint ep = new IPEndPoint (ip, 1254);
2023                         
2024                         listen.Bind (ep);
2025                         listen.Listen (1);
2026                         
2027                         sock.Connect (ip, 1254);
2028                         
2029                         Assert.AreEqual (true, sock.Connected, "BeginDisconnect #1");
2030                         
2031                         sock.Shutdown (SocketShutdown.Both);
2032
2033                         BDCalledBack.Reset ();
2034                         BDDisconnected = false;
2035                         
2036                         sock.BeginDisconnect (false, BDCallback, sock);
2037                 
2038                         if (BDCalledBack.WaitOne (2000, false) == false) {
2039                                 Assert.Fail ("BeginDisconnect wait timed out");
2040                         }
2041                         
2042                         Assert.AreEqual (true, BDDisconnected, "BeginDisconnect #2");
2043                         Assert.AreEqual (false, sock.Connected, "BeginDisconnect #3");
2044                         
2045                         sock.Close ();
2046                         listen.Close ();
2047                 }
2048                 
2049                 [Test]
2050                 public void BeginReceiveSocketError ()
2051                 {
2052                 }
2053                 
2054                 [Test]
2055                 public void BeginReceiveGeneric ()
2056                 {
2057                 }
2058                 
2059                 [Test]
2060                 public void BeginReceiveGenericSocketError ()
2061                 {
2062                 }
2063                 
2064                 private static void BSCallback (IAsyncResult asyncResult)
2065                 {
2066                         Socket sock = (Socket)asyncResult.AsyncState;
2067                         
2068                         sock.EndSend (asyncResult);
2069                 }
2070                 
2071                 [Test]
2072                 public void BeginSendNotConnected ()
2073                 {
2074                         Socket sock = new Socket (AddressFamily.InterNetwork,
2075                                                   SocketType.Stream,
2076                                                   ProtocolType.Tcp);
2077                         
2078                         byte[] send_bytes = new byte[] {10, 11, 12, 13};
2079                         
2080                         try {
2081                                 sock.BeginSend (send_bytes, 0,
2082                                                 send_bytes.Length,
2083                                                 SocketFlags.None, BSCallback,
2084                                                 sock);
2085                                 Assert.Fail ("BeginSendNotConnected #1");
2086                         } catch (SocketException ex) {
2087                                 Assert.AreEqual (10057, ex.ErrorCode, "BeginSendNotConnected #2");
2088                         } catch {
2089                                 Assert.Fail ("BeginSendNotConnected #3");
2090                         } finally {
2091                                 sock.Close ();
2092                         }
2093                 }
2094                 
2095                 [Test]
2096                 public void BeginSendSocketError ()
2097                 {
2098                 }
2099                 
2100                 [Test]
2101                 public void BeginSendGeneric ()
2102                 {
2103                 }
2104                 
2105                 [Test]
2106                 public void BeginSendGenericSocketError ()
2107                 {
2108                 }
2109                 
2110                 [Test]
2111                 public void BindTwice ()
2112                 {
2113                         Socket sock = new Socket (AddressFamily.InterNetwork,
2114                                                   SocketType.Stream,
2115                                                   ProtocolType.Tcp);
2116                         IPEndPoint ep1 = new IPEndPoint (IPAddress.Loopback,
2117                                                         1256);
2118                         IPEndPoint ep2 = new IPEndPoint (IPAddress.Loopback,
2119                                                          1257);
2120                         
2121                         sock.Bind (ep1);
2122                         
2123                         try {
2124                                 sock.Bind (ep2);
2125                                 Assert.Fail ("BindTwice #1");
2126                         } catch (SocketException ex) {
2127                                 Assert.AreEqual (10022, ex.ErrorCode, "BindTwice #2");
2128                         } catch {
2129                                 Assert.Fail ("BindTwice #3");
2130                         } finally {
2131                                 sock.Close ();
2132                         }
2133                 }
2134                 
2135                 [Test]
2136                 public void Close ()
2137                 {
2138                         Socket sock = new Socket (AddressFamily.InterNetwork,
2139                                                   SocketType.Stream,
2140                                                   ProtocolType.Tcp);
2141                         Socket listen = new Socket (AddressFamily.InterNetwork,
2142                                                     SocketType.Stream,
2143                                                     ProtocolType.Tcp);
2144                         IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
2145                                                         1258);
2146                         
2147                         listen.Bind (ep);
2148                         listen.Listen (1);
2149                         
2150                         sock.Connect (ep);
2151
2152                         Assert.AreEqual (true, sock.Connected, "Close #1");
2153                         
2154                         sock.Close (2);
2155                         
2156                         Thread.Sleep (3000);
2157                         
2158                         Assert.AreEqual (false, sock.Connected, "Close #2");
2159                         
2160                         listen.Close ();
2161                 }
2162                 
2163                 [Test]
2164                 public void ConnectAddressPort ()
2165                 {
2166                         Socket sock = new Socket (AddressFamily.InterNetwork,
2167                                                   SocketType.Stream,
2168                                                   ProtocolType.Tcp);
2169                         Socket listen = new Socket (AddressFamily.InterNetwork,
2170                                                     SocketType.Stream,
2171                                                     ProtocolType.Tcp);
2172                         IPAddress ip = IPAddress.Loopback;
2173                         IPEndPoint ep = new IPEndPoint (ip, 1249);
2174
2175                         listen.Bind (ep);
2176                         listen.Listen (1);
2177                         
2178                         sock.Connect (ip, 1249);
2179                         
2180                         Assert.AreEqual (true, sock.Connected, "ConnectAddressPort #1");
2181                         
2182                         sock.Close ();
2183                         listen.Close ();
2184                 }
2185
2186                 [Test]
2187                 public void ConnectAddressPortNull ()
2188                 {
2189                         Socket sock = new Socket (AddressFamily.InterNetwork,
2190                                                   SocketType.Stream,
2191                                                   ProtocolType.Tcp);
2192                         IPAddress ip = null;
2193
2194                         try {
2195                                 sock.Connect (ip, 1249);
2196                                 Assert.Fail ("ConnectAddressPortNull #1");
2197                         } catch (ArgumentNullException) {
2198                         } catch {
2199                                 Assert.Fail ("ConnectAddressPortNull #2");
2200                         } finally {
2201                                 sock.Close ();
2202                         }
2203                 }
2204
2205                 [Test]
2206                 public void ConnectAddressPortListen ()
2207                 {
2208                         Socket sock = new Socket (AddressFamily.InterNetwork,
2209                                                   SocketType.Stream,
2210                                                   ProtocolType.Tcp);
2211                         IPAddress ip = IPAddress.Loopback;
2212                         IPEndPoint ep = new IPEndPoint (ip, 1250);
2213
2214                         sock.Bind (ep);
2215                         sock.Listen (1);
2216                         
2217                         try {
2218                                 sock.Connect (ip, 1250);
2219                                 Assert.Fail ("ConnectAddressPortListen #1");
2220                         } catch (InvalidOperationException) {
2221                         } catch {
2222                                 Assert.Fail ("ConnectAddressPortListen #2");
2223                         } finally {
2224                                 sock.Close ();
2225                         }
2226                 }
2227                 
2228                 [Test]
2229                 [ExpectedException (typeof(ObjectDisposedException))]
2230                 public void ConnectAddressPortClosed ()
2231                 {
2232                         Socket sock = new Socket (AddressFamily.InterNetwork,
2233                                                   SocketType.Stream,
2234                                                   ProtocolType.Tcp);
2235                         IPAddress ip = IPAddress.Loopback;
2236                         
2237                         sock.Close ();
2238                         
2239                         sock.Connect (ip, 1250);
2240                 }
2241                 
2242                 [Test]
2243                 [Category ("NotOnMac")] // MacOSX trashes the fd after the failed connect attempt to 127.0.0.4
2244                 /*
2245                  * This is not a Mono bug.
2246                  * 
2247                  * By default, only 127.0.0.1 is enabled and you must explicitly
2248                  * enable additional addresses using 'sudo ifconfig lo0 alias 127.0.0.1'.
2249                  * 
2250                  * I tested this on Mac OS 10.7.4; a 'ping 127.0.0.2' does not work
2251                  * until I add that alias.
2252                  * 
2253                  * However, after doing so, Mac OS treats these as separate addresses, ie. attempting
2254                  * to connect to 127.0.0.4 yields a connection refused.
2255                  * 
2256                  * When using Connect(), the .NET runtime also throws an exception if connecting to
2257                  * any of the IP addresses fails.  This is different with BeginConnect()/EndConnect()
2258                  * which succeeds when it can connect to at least one of the addresses.
2259                  * 
2260                  */
2261                 public void ConnectMultiple ()
2262                 {
2263                         Socket sock = new Socket (AddressFamily.InterNetwork,
2264                                                   SocketType.Stream,
2265                                                   ProtocolType.Tcp);
2266                         Socket listen = new Socket (AddressFamily.InterNetwork,
2267                                                     SocketType.Stream,
2268                                                     ProtocolType.Tcp);
2269                         IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
2270                                                         1251);
2271                         IPAddress[] ips = new IPAddress[4];
2272                         
2273                         ips[0] = IPAddress.Parse ("127.0.0.4");
2274                         ips[1] = IPAddress.Parse ("127.0.0.3");
2275                         ips[2] = IPAddress.Parse ("127.0.0.2");
2276                         ips[3] = IPAddress.Parse ("127.0.0.1");
2277
2278                         listen.Bind (ep);
2279                         listen.Listen (1);
2280                         
2281                         sock.Connect (ips, 1251);
2282                         
2283                         Assert.AreEqual (true, sock.Connected, "ConnectMultiple #1");
2284                         Assert.AreEqual (AddressFamily.InterNetwork, sock.RemoteEndPoint.AddressFamily, "ConnectMultiple #2");
2285                         IPEndPoint remep = (IPEndPoint)sock.RemoteEndPoint;
2286                         
2287                         Assert.AreEqual (IPAddress.Loopback, remep.Address, "ConnectMultiple #2");
2288                         
2289                         sock.Close ();
2290                         listen.Close ();
2291                 }
2292
2293                 [Test]
2294                 public void ConnectMultiple2 ()
2295                 {
2296                         Socket sock = new Socket (AddressFamily.InterNetwork,
2297                                                   SocketType.Stream,
2298                                                   ProtocolType.Tcp);
2299                         Socket listen = new Socket (AddressFamily.InterNetwork,
2300                                                     SocketType.Stream,
2301                                                     ProtocolType.Tcp);
2302
2303                         // Need at least two addresses.
2304                         var ips = Dns.GetHostAddresses (string.Empty);
2305                         if (ips.Length < 1)
2306                                 return;
2307
2308                         var allIps = new IPAddress [ips.Length + 1];
2309                         allIps [0] = IPAddress.Loopback;
2310                         ips.CopyTo (allIps, 1);
2311
2312                         /*
2313                          * Bind to IPAddress.Any; Connect() will fail unless it can
2314                          * connect to all the addresses in allIps.
2315                          */
2316                         IPEndPoint ep = new IPEndPoint (IPAddress.Any, 1251);
2317
2318                         listen.Bind (ep);
2319                         listen.Listen (1);
2320                         
2321                         sock.Connect (allIps, 1251);
2322                         
2323                         Assert.AreEqual (true, sock.Connected, "ConnectMultiple2 #1");
2324                         Assert.AreEqual (AddressFamily.InterNetwork, sock.RemoteEndPoint.AddressFamily, "ConnectMultiple2 #2");
2325                         IPEndPoint remep = (IPEndPoint)sock.RemoteEndPoint;
2326
2327                         Assert.AreEqual (IPAddress.Loopback, remep.Address, "ConnectMultiple2 #3");
2328                         
2329                         sock.Close ();
2330                         listen.Close ();
2331                 }
2332
2333                 [Test]
2334                 public void ConnectMultipleNull ()
2335                 {
2336                         Socket sock = new Socket (AddressFamily.InterNetwork,
2337                                                   SocketType.Stream,
2338                                                   ProtocolType.Tcp);
2339                         IPAddress[] ips = null;
2340                         
2341                         try {
2342                                 sock.Connect (ips, 1251);
2343                                 Assert.Fail ("ConnectMultipleNull #1");
2344                         } catch (ArgumentNullException) {
2345                         } catch {
2346                                 Assert.Fail ("ConnectMultipleNull #2");
2347                         } finally {
2348                                 sock.Close ();
2349                         }
2350                 }
2351
2352                 [Test]
2353                 public void ConnectMultipleListen ()
2354                 {
2355                         Socket sock = new Socket (AddressFamily.InterNetwork,
2356                                                   SocketType.Stream,
2357                                                   ProtocolType.Tcp);
2358                         IPAddress[] ips = new IPAddress[4];
2359                         IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
2360                                                         1252);
2361                         
2362                         ips[0] = IPAddress.Parse ("127.0.0.4");
2363                         ips[1] = IPAddress.Parse ("127.0.0.3");
2364                         ips[2] = IPAddress.Parse ("127.0.0.2");
2365                         ips[3] = IPAddress.Parse ("127.0.0.1");
2366                         
2367                         sock.Bind (ep);
2368                         sock.Listen (1);
2369                         
2370                         try {
2371                                 sock.Connect (ips, 1252);
2372                                 Assert.Fail ("ConnectMultipleListen #1");
2373                         } catch (InvalidOperationException) {
2374                         } catch {
2375                                 Assert.Fail ("ConnectMultipleListen #2");
2376                         } finally {
2377                                 sock.Close ();
2378                         }
2379                 }
2380                 
2381                 [Test]
2382                 [ExpectedException (typeof(ObjectDisposedException))]
2383                 public void ConnectMultipleClosed ()
2384                 {
2385                         Socket sock = new Socket (AddressFamily.InterNetwork,
2386                                                   SocketType.Stream,
2387                                                   ProtocolType.Tcp);
2388                         IPAddress[] ips = new IPAddress[4];
2389                         
2390                         ips[0] = IPAddress.Parse ("127.0.0.4");
2391                         ips[1] = IPAddress.Parse ("127.0.0.3");
2392                         ips[2] = IPAddress.Parse ("127.0.0.2");
2393                         ips[3] = IPAddress.Parse ("127.0.0.1");
2394                         
2395                         sock.Close ();
2396                         
2397                         sock.Connect (ips, 1252);
2398                 }
2399                 
2400                 [Test]
2401                 public void ConnectHostPortNull ()
2402                 {
2403                         Socket sock = new Socket (AddressFamily.InterNetwork,
2404                                                   SocketType.Stream,
2405                                                   ProtocolType.Tcp);
2406                         
2407                         try {
2408                                 sock.Connect ((string)null, 0);
2409                                 Assert.Fail ("ConnectHostPort #1");
2410                         } catch (ArgumentNullException) {
2411                         } catch {
2412                                 Assert.Fail ("ConnectHostPort #2");
2413                         } finally {
2414                                 sock.Close ();
2415                         }
2416                 }
2417
2418                 [Test]
2419                 public void ConnectHostPortListen ()
2420                 {
2421                         Socket sock = new Socket (AddressFamily.InterNetwork,
2422                                                   SocketType.Stream,
2423                                                   ProtocolType.Tcp);
2424                         IPAddress ip = IPAddress.Loopback;
2425                         IPEndPoint ep = new IPEndPoint (ip, 1253);
2426                         
2427                         sock.Bind (ep);
2428                         sock.Listen (1);
2429                         
2430                         try {
2431                                 sock.Connect ("localhost", 1253);
2432                                 Assert.Fail ("ConnectHostPortListen #1");
2433                         } catch (InvalidOperationException) {
2434                         } catch {
2435                                 Assert.Fail ("ConnectHostPortListen #2");
2436                         } finally {
2437                                 sock.Close ();
2438                         }
2439                 }
2440
2441                 [Test]
2442                 [Category ("NotWorking")] // Need to pick a non-IP AddressFamily that "works" on both mono and ms, this one only works on ms
2443                 public void ConnectHostPortNotIP ()
2444                 {
2445                         Socket sock = new Socket (AddressFamily.NetBios,
2446                                                   SocketType.Seqpacket,
2447                                                   ProtocolType.Unspecified);
2448                         
2449                         try {
2450                                 sock.Connect ("localhost", 0);
2451                                 Assert.Fail ("ConnectHostPortNotIP #1");
2452                         } catch (NotSupportedException) {
2453                         } catch {
2454                                 Assert.Fail ("ConnectHostPortNotIP #2");
2455                         } finally {
2456                                 sock.Close ();
2457                         }
2458                 }
2459
2460                 [Test]
2461                 [ExpectedException (typeof(ObjectDisposedException))]
2462                 public void ConnectHostPortClosed ()
2463                 {
2464                         Socket sock = new Socket (AddressFamily.InterNetwork,
2465                                                   SocketType.Stream,
2466                                                   ProtocolType.Tcp);
2467                         
2468                         sock.Close ();
2469                         
2470                         sock.Connect ("localhost", 0);
2471                 }
2472                 
2473                 [Test]
2474                 [Category ("NotDotNet")] // "Needs XP or later"
2475                 public void Disconnect ()
2476                 {
2477                         Socket sock = new Socket (AddressFamily.InterNetwork,
2478                                                   SocketType.Stream,
2479                                                   ProtocolType.Tcp);
2480                         Socket listen = new Socket (AddressFamily.InterNetwork,
2481                                                     SocketType.Stream,
2482                                                     ProtocolType.Tcp);
2483                         IPAddress ip = IPAddress.Loopback;
2484                         IPEndPoint ep = new IPEndPoint (ip, 1255);
2485                         
2486                         listen.Bind (ep);
2487                         listen.Listen (1);
2488                         
2489                         sock.Connect (ip, 1255);
2490                         
2491                         Assert.AreEqual (true, sock.Connected, "Disconnect #1");
2492                         
2493                         sock.Shutdown (SocketShutdown.Both);
2494
2495                         sock.Disconnect (false);
2496
2497                         Assert.AreEqual (false, sock.Connected, "BeginDisconnect #3");
2498                         
2499                         sock.Close ();
2500                         listen.Close ();
2501                 }
2502                 
2503                 [Test]
2504                 public void DuplicateAndClose ()
2505                 {
2506                 }
2507                 
2508                 [Test]
2509                 public void IOControl ()
2510                 {
2511                 }
2512                 
2513                 [Test]
2514                 public void ReceiveGeneric ()
2515                 {
2516                         int i;
2517
2518                         IPEndPoint endpoint = new IPEndPoint(IPAddress.Loopback, 1258);
2519
2520                         Socket listensock = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
2521                         listensock.Bind (endpoint);
2522                         listensock.Listen(1);
2523
2524                         Socket sendsock = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
2525                         sendsock.Connect(endpoint);
2526
2527                         Socket clientsock = listensock.Accept();
2528                         
2529                         byte[] sendbuf = new byte[256];
2530
2531                         for(i = 0; i < 256; i++) {
2532                                 sendbuf[i] = (byte)i;
2533                         }
2534                         for (i = 4; i < 6; i++) {
2535                                 Assert.AreEqual (sendbuf[i], (byte)i,
2536                                                  "#1/" + i.ToString());
2537                         }
2538
2539                         SocketError err;
2540                         sendsock.Send (sendbuf, 0, 256, SocketFlags.None,
2541                                        out err);
2542
2543
2544                         byte[] recvbuf = new byte[256];
2545                         List<ArraySegment<byte>> recvbuflist = new List<ArraySegment<byte>>(2);
2546                         recvbuflist.Add(new ArraySegment<byte>(recvbuf, 4, 2));
2547                         recvbuflist.Add(new ArraySegment<byte>(recvbuf, 20, 230));
2548                         
2549                         clientsock.Receive (recvbuflist);
2550
2551                         /* recvbuf should now hold the first 2 bytes
2552                          * of sendbuf from pos 4, and the next 230
2553                          * bytes of sendbuf from pos 20
2554                          */
2555
2556                         for (i = 0; i < 2; i++) {
2557                                 Assert.AreEqual (sendbuf[i], recvbuf[i + 4],
2558                                                  "#2/" + i.ToString());
2559                         }
2560                         for (i = 2; i < 232; i++) {
2561                                 Assert.AreEqual (sendbuf[i], recvbuf[i + 18],
2562                                                  "#2/" + i.ToString());
2563                         }
2564
2565                         sendsock.Close ();
2566                         clientsock.Close ();
2567                         listensock.Close ();
2568                 }
2569                 
2570                 [Test]
2571                 public void SendGeneric ()
2572                 {
2573                         int i;
2574
2575                         IPEndPoint endpoint = new IPEndPoint(IPAddress.Loopback, 1259);
2576
2577                         Socket listensock = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
2578                         listensock.Bind (endpoint);
2579                         listensock.Listen(1);
2580
2581                         Socket sendsock = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
2582                         sendsock.Connect(endpoint);
2583
2584                         Socket clientsock = listensock.Accept();
2585
2586                         byte[] sendbuf = new byte[256];
2587                         List<ArraySegment<byte>> sendbuflist = new List<ArraySegment<byte>>(2);
2588
2589                         sendbuflist.Add(new ArraySegment<byte>(sendbuf, 4, 2));
2590                         sendbuflist.Add(new ArraySegment<byte>(sendbuf, 20, 230));
2591
2592                         for(i = 0; i < 256; i++) {
2593                                 sendbuf[i] = (byte)i;
2594                         }
2595                         for (i = 4; i < 6; i++) {
2596                                 Assert.AreEqual (sendbuf[i], (byte)i,
2597                                                  "#1/" + i.ToString());
2598                         }
2599
2600                         SocketError err;
2601                         sendsock.Send (sendbuflist, SocketFlags.None, out err);
2602
2603                         
2604                         byte[] recvbuf = new byte[256];
2605
2606                         clientsock.Receive (recvbuf);
2607
2608                         /* The first 2 bytes of recvbuf should now
2609                          * hold 2 bytes of sendbuf from pos 4, and the
2610                          * next 230 bytes of recvbuf should be sendbuf
2611                          * from pos 20
2612                          */
2613
2614                         for (i = 0; i < 2; i++) {
2615                                 Assert.AreEqual (recvbuf[i], sendbuf[i + 4],
2616                                                  "#2/" + i.ToString());
2617                         }
2618                         for (i = 2; i < 232; i++) {
2619                                 Assert.AreEqual (recvbuf[i], sendbuf[i + 18],
2620                                                  "#2/" + i.ToString());
2621                         }
2622
2623                         sendsock.Close ();
2624                         clientsock.Close ();
2625                         listensock.Close ();
2626                 }
2627
2628                 [Test]
2629                 public void ListenNotBound ()
2630                 {
2631                         Socket sock = new Socket (AddressFamily.InterNetwork,
2632                                                   SocketType.Stream,
2633                                                   ProtocolType.Tcp);
2634                         
2635                         try {
2636                                 sock.Listen (1);
2637                                 Assert.Fail ("ListenNotBound #1");
2638                         } catch (SocketException ex) {
2639                                 Assert.AreEqual (10022, ex.ErrorCode, "ListenNotBound #2");
2640                         } catch {
2641                                 Assert.Fail ("ListenNotBound #3");
2642                         } finally {
2643                                 sock.Close ();
2644                         }
2645                 }
2646 #endif
2647
2648                 static Socket CWRSocket;
2649                 static bool CWRReceiving = true;
2650                 static ManualResetEvent CWRReady = new ManualResetEvent (false);
2651                 
2652                 private static void CWRReceiveThread ()
2653                 {
2654                         byte[] buf = new byte[256];
2655                         
2656                         try {
2657                                 CWRSocket.Receive (buf);
2658                         } catch (SocketException) {
2659                                 CWRReceiving = false;
2660                         }
2661
2662                         CWRReady.Set ();
2663                 }
2664                 
2665                 [Test]
2666                 public void CloseWhileReceiving ()
2667                 {
2668                         CWRSocket = new Socket (AddressFamily.InterNetwork,
2669                                                 SocketType.Dgram,
2670                                                 ProtocolType.Udp);
2671                         CWRSocket.Bind (new IPEndPoint (IPAddress.Loopback,
2672                                                         1256));
2673                         
2674                         Thread recv_thread = new Thread (new ThreadStart (CWRReceiveThread));
2675                         CWRReady.Reset ();
2676                         recv_thread.Start ();
2677                         Thread.Sleep (250);     /* Wait for the thread to be already receiving */
2678
2679                         CWRSocket.Close ();
2680                         if (CWRReady.WaitOne (1000, false) == false) {
2681                                 Assert.Fail ("CloseWhileReceiving wait timed out");
2682                         }
2683                         
2684                         Assert.IsFalse (CWRReceiving);
2685                 }
2686
2687                 static bool RRCLastRead = false;
2688                 static ManualResetEvent RRCReady = new ManualResetEvent (false);
2689                 
2690                 private static void RRCClientThread ()
2691                 {
2692                         byte[] bytes = new byte[8];
2693                         int readbyte;
2694                         
2695                         Socket sock = new Socket (AddressFamily.InterNetwork,
2696                                                   SocketType.Stream,
2697                                                   ProtocolType.Tcp);
2698                         sock.Connect (new IPEndPoint (IPAddress.Loopback,
2699                                                       1257));
2700                         
2701                         NetworkStream stream = new NetworkStream (sock);
2702
2703                         readbyte = stream.ReadByte ();
2704                         Assert.AreEqual (0, readbyte, "ReceiveRemoteClosed #1");
2705                         
2706                         stream.Read (bytes, 0, 0);
2707
2708                         readbyte = stream.ReadByte ();
2709                         Assert.AreEqual (0, readbyte, "ReceiveRemoteClosed #2");
2710                         
2711                         stream.Read (bytes, 0, 0);
2712
2713                         readbyte = stream.ReadByte ();
2714                         Assert.AreEqual (-1, readbyte, "ReceiveRemoteClosed #3");
2715
2716                         sock.Close ();
2717
2718                         RRCLastRead = true;
2719                         RRCReady.Set ();
2720                 }
2721
2722                 [Test] // Receive (Byte [])
2723                 public void Receive1_Buffer_Null ()
2724                 {
2725                         Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
2726                                 ProtocolType.Tcp);
2727
2728                         try {
2729                                 s.Receive ((byte []) null);
2730                                 Assert.Fail ("#1");
2731                         } catch (ArgumentNullException ex) {
2732                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
2733                                 Assert.IsNull (ex.InnerException, "#3");
2734                                 Assert.IsNotNull (ex.Message, "#4");
2735                                 Assert.AreEqual ("buffer", ex.ParamName, "#5");
2736                         } finally {
2737                                 s.Close ();
2738                         }
2739                 }
2740
2741                 [Test] // Receive (Byte [])
2742                 public void Receive1_Socket_Closed ()
2743                 {
2744                         Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
2745                                 ProtocolType.Tcp);
2746                         s.Close ();
2747
2748                         try {
2749                                 s.Receive ((byte []) null);
2750                                 Assert.Fail ("#1");
2751                         } catch (ObjectDisposedException ex) {
2752                                 // Cannot access a disposed object
2753                                 Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
2754                                 Assert.IsNull (ex.InnerException, "#3");
2755                                 Assert.IsNotNull (ex.Message, "#4");
2756                                 Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
2757                         }
2758                 }
2759
2760                 [Test] // Receive (Byte [], SocketFlags)
2761                 public void Receive2_Buffer_Null ()
2762                 {
2763                         Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
2764                                 ProtocolType.Tcp);
2765
2766                         try {
2767                                 s.Receive ((byte []) null, (SocketFlags) 666);
2768                                 Assert.Fail ("#1");
2769                         } catch (ArgumentNullException ex) {
2770                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
2771                                 Assert.IsNull (ex.InnerException, "#3");
2772                                 Assert.IsNotNull (ex.Message, "#4");
2773                                 Assert.AreEqual ("buffer", ex.ParamName, "#5");
2774                         } finally {
2775                                 s.Close ();
2776                         }
2777                 }
2778
2779                 [Test] // Receive (Byte [], SocketFlags)
2780                 public void Receive2_Socket_Closed ()
2781                 {
2782                         Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
2783                                 ProtocolType.Tcp);
2784                         s.Close ();
2785
2786                         try {
2787                                 s.Receive ((byte []) null, (SocketFlags) 666);
2788                                 Assert.Fail ("#1");
2789                         } catch (ObjectDisposedException ex) {
2790                                 // Cannot access a disposed object
2791                                 Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
2792                                 Assert.IsNull (ex.InnerException, "#3");
2793                                 Assert.IsNotNull (ex.Message, "#4");
2794                                 Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
2795                         }
2796                 }
2797
2798                 [Test] // Receive (Byte [], Int32, SocketFlags)
2799                 public void Receive3_Buffer_Null ()
2800                 {
2801                         Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
2802                                 ProtocolType.Tcp);
2803
2804                         try {
2805                                 s.Receive ((byte []) null, 0, (SocketFlags) 666);
2806                                 Assert.Fail ("#1");
2807                         } catch (ArgumentNullException ex) {
2808                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
2809                                 Assert.IsNull (ex.InnerException, "#3");
2810                                 Assert.IsNotNull (ex.Message, "#4");
2811                                 Assert.AreEqual ("buffer", ex.ParamName, "#5");
2812                         } finally {
2813                                 s.Close ();
2814                         }
2815                 }
2816
2817                 [Test] // Receive (Byte [], Int32, SocketFlags)
2818                 public void Receive3_Socket_Closed ()
2819                 {
2820                         Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
2821                                 ProtocolType.Tcp);
2822                         s.Close ();
2823
2824                         try {
2825                                 s.Receive ((byte []) null, 0, (SocketFlags) 666);
2826                                 Assert.Fail ("#1");
2827                         } catch (ObjectDisposedException ex) {
2828                                 // Cannot access a disposed object
2829                                 Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
2830                                 Assert.IsNull (ex.InnerException, "#3");
2831                                 Assert.IsNotNull (ex.Message, "#4");
2832                                 Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
2833                         }
2834                 }
2835
2836                 [Test] // Receive (Byte [], Int32, Int32, SocketFlags)
2837                 public void Receive4_Buffer_Null ()
2838                 {
2839                         Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
2840                                 ProtocolType.Tcp);
2841
2842                         try {
2843                                 s.Receive ((byte []) null, 0, 0, (SocketFlags) 666);
2844                                 Assert.Fail ("#1");
2845                         } catch (ArgumentNullException ex) {
2846                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
2847                                 Assert.IsNull (ex.InnerException, "#3");
2848                                 Assert.IsNotNull (ex.Message, "#4");
2849                                 Assert.AreEqual ("buffer", ex.ParamName, "#5");
2850                         } finally {
2851                                 s.Close ();
2852                         }
2853                 }
2854
2855                 [Test] // Receive (Byte [], Int32, Int32, SocketFlags)
2856                 public void Receive4_Socket_Closed ()
2857                 {
2858                         Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
2859                                 ProtocolType.Tcp);
2860                         s.Close ();
2861
2862                         try {
2863                                 s.Receive ((byte []) null, 0, 0, (SocketFlags) 666);
2864                                 Assert.Fail ("#1");
2865                         } catch (ObjectDisposedException ex) {
2866                                 // Cannot access a disposed object
2867                                 Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
2868                                 Assert.IsNull (ex.InnerException, "#3");
2869                                 Assert.IsNotNull (ex.Message, "#4");
2870                                 Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
2871                         }
2872                 }
2873
2874 #if NET_2_0
2875                 [Test] // Receive (Byte [], Int32, Int32, SocketFlags, out SocketError)
2876                 public void Receive5_Buffer_Null ()
2877                 {
2878                         Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
2879                                 ProtocolType.Tcp);
2880
2881                         SocketError error;
2882                         try {
2883                                 s.Receive ((byte []) null, 0, 0, SocketFlags.None, out error);
2884                                 Assert.Fail ("#1");
2885                         } catch (ArgumentNullException ex) {
2886                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
2887                                 Assert.IsNull (ex.InnerException, "#3");
2888                                 Assert.IsNotNull (ex.Message, "#4");
2889                                 Assert.AreEqual ("buffer", ex.ParamName, "#5");
2890                         } finally {
2891                                 s.Close ();
2892                         }
2893                 }
2894
2895                 [Test] // Receive (Byte [], Int32, Int32, SocketFlags, out SocketError)
2896                 public void Receive5_Socket_Closed ()
2897                 {
2898                         Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
2899                                 ProtocolType.Tcp);
2900                         s.Close ();
2901
2902                         SocketError error;
2903                         try {
2904                                 s.Receive ((byte []) null, 0, 0, SocketFlags.None, out error);
2905                                 Assert.Fail ("#1");
2906                         } catch (ObjectDisposedException ex) {
2907                                 // Cannot access a disposed object
2908                                 Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
2909                                 Assert.IsNull (ex.InnerException, "#3");
2910                                 Assert.IsNotNull (ex.Message, "#4");
2911                                 Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
2912                         }
2913                 }
2914
2915                 [Test] // Receive (IList<ArraySegment<Byte>>)
2916                 public void Receive6_Buffers_Null ()
2917                 {
2918                         Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
2919                                 ProtocolType.Tcp);
2920
2921                         try {
2922                                 s.Receive ((IList<ArraySegment<byte>>) null);
2923                                 Assert.Fail ("#1");
2924                         } catch (ArgumentNullException ex) {
2925                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
2926                                 Assert.IsNull (ex.InnerException, "#3");
2927                                 Assert.IsNotNull (ex.Message, "#4");
2928                                 Assert.AreEqual ("buffers", ex.ParamName, "#5");
2929                         } finally {
2930                                 s.Close ();
2931                         }
2932                 }
2933
2934                 [Test] // Receive (IList<ArraySegment<Byte>>)
2935                 public void Receive6_Socket_Closed ()
2936                 {
2937                         Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
2938                                 ProtocolType.Tcp);
2939                         s.Close ();
2940
2941                         try {
2942                                 s.Receive ((IList<ArraySegment<byte>>) null);
2943                                 Assert.Fail ("#1");
2944                         } catch (ObjectDisposedException ex) {
2945                                 // Cannot access a disposed object
2946                                 Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
2947                                 Assert.IsNull (ex.InnerException, "#3");
2948                                 Assert.IsNotNull (ex.Message, "#4");
2949                                 Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
2950                         }
2951                 }
2952
2953                 [Test] // Receive (IList<ArraySegment<Byte>>, SocketFlags)
2954                 public void Receive7_Buffers_Null ()
2955                 {
2956                         Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
2957                                 ProtocolType.Tcp);
2958
2959                         try {
2960                                 s.Receive ((IList<ArraySegment<byte>>) null, (SocketFlags) 666);
2961                                 Assert.Fail ("#1");
2962                         } catch (ArgumentNullException ex) {
2963                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
2964                                 Assert.IsNull (ex.InnerException, "#3");
2965                                 Assert.IsNotNull (ex.Message, "#4");
2966                                 Assert.AreEqual ("buffers", ex.ParamName, "#5");
2967                         } finally {
2968                                 s.Close ();
2969                         }
2970                 }
2971
2972                 [Test] // Receive (IList<ArraySegment<Byte>>, SocketFlags)
2973                 public void Receive7_Socket_Closed ()
2974                 {
2975                         Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
2976                                 ProtocolType.Tcp);
2977                         s.Close ();
2978
2979                         try {
2980                                 s.Receive ((IList<ArraySegment<byte>>) null, (SocketFlags) 666);
2981                                 Assert.Fail ("#1");
2982                         } catch (ObjectDisposedException ex) {
2983                                 // Cannot access a disposed object
2984                                 Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
2985                                 Assert.IsNull (ex.InnerException, "#3");
2986                                 Assert.IsNotNull (ex.Message, "#4");
2987                                 Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
2988                         }
2989                 }
2990
2991                 [Test] // Receive (IList<ArraySegment<Byte>>, SocketFlags, out SocketError)
2992                 public void Receive8_Buffers_Null ()
2993                 {
2994                         Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
2995                                 ProtocolType.Tcp);
2996
2997                         SocketError error;
2998                         try {
2999                                 s.Receive ((IList<ArraySegment<byte>>) null, (SocketFlags) 666,
3000                                         out error);
3001                                 Assert.Fail ("#1");
3002                         } catch (ArgumentNullException ex) {
3003                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
3004                                 Assert.IsNull (ex.InnerException, "#3");
3005                                 Assert.IsNotNull (ex.Message, "#4");
3006                                 Assert.AreEqual ("buffers", ex.ParamName, "#5");
3007                         } finally {
3008                                 s.Close ();
3009                         }
3010                 }
3011
3012                 [Test] // Receive (IList<ArraySegment<Byte>>, SocketFlags, out SocketError)
3013                 public void Receive8_Socket_Closed ()
3014                 {
3015                         Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
3016                                 ProtocolType.Tcp);
3017                         s.Close ();
3018
3019                         SocketError error;
3020                         try {
3021                                 s.Receive ((IList<ArraySegment<byte>>) null, (SocketFlags) 666,
3022                                         out error);
3023                                 Assert.Fail ("#1");
3024                         } catch (ObjectDisposedException ex) {
3025                                 // Cannot access a disposed object
3026                                 Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
3027                                 Assert.IsNull (ex.InnerException, "#3");
3028                                 Assert.IsNotNull (ex.Message, "#4");
3029                                 Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
3030                         } finally {
3031                                 s.Close ();
3032                         }
3033                 }
3034 #endif
3035
3036                 [Test] // ReceiveFrom (Byte [], ref EndPoint)
3037                 public void ReceiveFrom1_Buffer_Null ()
3038                 {
3039                         Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
3040                                 ProtocolType.Tcp);
3041
3042                         EndPoint remoteEP = new IPEndPoint (IPAddress.Loopback, 8001);
3043                         try {
3044                                 s.ReceiveFrom ((Byte []) null, ref remoteEP);
3045                                 Assert.Fail ("#1");
3046                         } catch (ArgumentNullException ex) {
3047                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
3048                                 Assert.IsNull (ex.InnerException, "#3");
3049                                 Assert.IsNotNull (ex.Message, "#4");
3050                                 Assert.AreEqual ("buffer", ex.ParamName, "#5");
3051                         } finally {
3052                                 s.Close ();
3053                         }
3054                 }
3055
3056                 [Test] // ReceiveFrom (Byte [], ref EndPoint)
3057                 public void ReceiveFrom1_RemoteEP_Null ()
3058                 {
3059                         Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
3060                                 ProtocolType.Tcp);
3061
3062                         byte [] buffer = new byte [0];
3063                         EndPoint remoteEP = null;
3064                         try {
3065                                 s.ReceiveFrom (buffer, ref remoteEP);
3066                                 Assert.Fail ("#1");
3067                         } catch (ArgumentNullException ex) {
3068                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
3069                                 Assert.IsNull (ex.InnerException, "#3");
3070                                 Assert.IsNotNull (ex.Message, "#4");
3071                                 Assert.AreEqual ("remoteEP", ex.ParamName, "#5");
3072                         } finally {
3073                                 s.Close ();
3074                         }
3075                 }
3076
3077                 [Test] // ReceiveFrom (Byte [], ref EndPoint)
3078                 public void ReceiveFrom1_Socket_Closed ()
3079                 {
3080                         Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
3081                                 ProtocolType.Tcp);
3082                         s.Close ();
3083
3084                         EndPoint remoteEP = new IPEndPoint (IPAddress.Loopback, 8001);
3085                         try {
3086                                 s.ReceiveFrom ((Byte []) null, ref remoteEP);
3087                                 Assert.Fail ("#1");
3088                         } catch (ObjectDisposedException ex) {
3089                                 // Cannot access a disposed object
3090                                 Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
3091                                 Assert.IsNull (ex.InnerException, "#3");
3092                                 Assert.IsNotNull (ex.Message, "#4");
3093                                 Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
3094                         }
3095                 }
3096
3097                 [Test] // ReceiveFrom (Byte [], SocketFlags, ref EndPoint)
3098                 public void ReceiveFrom2_Buffer_Null ()
3099                 {
3100                         Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
3101                                 ProtocolType.Tcp);
3102
3103                         EndPoint remoteEP = new IPEndPoint (IPAddress.Loopback, 8001);
3104                         try {
3105                                 s.ReceiveFrom ((Byte []) null, (SocketFlags) 666, ref remoteEP);
3106                                 Assert.Fail ("#1");
3107                         } catch (ArgumentNullException ex) {
3108                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
3109                                 Assert.IsNull (ex.InnerException, "#3");
3110                                 Assert.IsNotNull (ex.Message, "#4");
3111                                 Assert.AreEqual ("buffer", ex.ParamName, "#5");
3112                         } finally {
3113                                 s.Close ();
3114                         }
3115                 }
3116
3117                 [Test] // ReceiveFrom (Byte [], SocketFlags, ref EndPoint)
3118                 public void ReceiveFrom2_RemoteEP_Null ()
3119                 {
3120                         Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
3121                                 ProtocolType.Tcp);
3122
3123                         byte [] buffer = new byte [5];
3124                         EndPoint remoteEP = null;
3125                         try {
3126                                 s.ReceiveFrom (buffer, (SocketFlags) 666, ref remoteEP);
3127                                 Assert.Fail ("#1");
3128                         } catch (ArgumentNullException ex) {
3129                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
3130                                 Assert.IsNull (ex.InnerException, "#3");
3131                                 Assert.IsNotNull (ex.Message, "#4");
3132                                 Assert.AreEqual ("remoteEP", ex.ParamName, "#5");
3133                         } finally {
3134                                 s.Close ();
3135                         }
3136                 }
3137
3138                 [Test] // ReceiveFrom (Byte [], SocketFlags, ref EndPoint)
3139                 public void ReceiveFrom2_Socket_Closed ()
3140                 {
3141                         Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
3142                                 ProtocolType.Tcp);
3143                         s.Close ();
3144
3145                         EndPoint remoteEP = new IPEndPoint (IPAddress.Loopback, 8001);
3146                         try {
3147                                 s.ReceiveFrom ((Byte []) null, (SocketFlags) 666, ref remoteEP);
3148                                 Assert.Fail ("#1");
3149                         } catch (ObjectDisposedException ex) {
3150                                 // Cannot access a disposed object
3151                                 Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
3152                                 Assert.IsNull (ex.InnerException, "#3");
3153                                 Assert.IsNotNull (ex.Message, "#4");
3154                                 Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
3155                         }
3156                 }
3157
3158                 [Test] // ReceiveFrom (Byte [], Int32, SocketFlags, ref EndPoint)
3159                 public void ReceiveFrom3_Buffer_Null ()
3160                 {
3161                         Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
3162                                 ProtocolType.Tcp);
3163
3164                         EndPoint remoteEP = new IPEndPoint (IPAddress.Loopback, 8001);
3165                         try {
3166                                 s.ReceiveFrom ((Byte []) null, -1, (SocketFlags) 666,
3167                                         ref remoteEP);
3168                                 Assert.Fail ("#1");
3169                         } catch (ArgumentNullException ex) {
3170                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
3171                                 Assert.IsNull (ex.InnerException, "#3");
3172                                 Assert.IsNotNull (ex.Message, "#4");
3173                                 Assert.AreEqual ("buffer", ex.ParamName, "#5");
3174                         } finally {
3175                                 s.Close ();
3176                         }
3177                 }
3178
3179                 [Test] // ReceiveFrom (Byte [], Int32, SocketFlags, ref EndPoint)
3180                 public void ReceiveFrom3_RemoteEP_Null ()
3181                 {
3182                         Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
3183                                 ProtocolType.Tcp);
3184
3185                         byte [] buffer = new byte [5];
3186                         EndPoint remoteEP = null;
3187                         try {
3188                                 s.ReceiveFrom (buffer, -1, (SocketFlags) 666, ref remoteEP);
3189                                 Assert.Fail ("#1");
3190                         } catch (ArgumentNullException ex) {
3191                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
3192                                 Assert.IsNull (ex.InnerException, "#3");
3193                                 Assert.IsNotNull (ex.Message, "#4");
3194                                 Assert.AreEqual ("remoteEP", ex.ParamName, "#5");
3195                         } finally {
3196                                 s.Close ();
3197                         }
3198                 }
3199
3200                 [Test] // ReceiveFrom (Byte [], Int32, SocketFlags, ref EndPoint)
3201                 public void ReceiveFrom3_Size_OutOfRange ()
3202                 {
3203                         Socket s;
3204                         byte [] buffer = new byte [5];
3205                         EndPoint remoteEP = new IPEndPoint (IPAddress.Loopback, 8001);
3206
3207                         // size negative
3208                         s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
3209                                                         ProtocolType.Tcp);
3210                         try {
3211                                 s.ReceiveFrom (buffer, -1, (SocketFlags) 666, ref remoteEP);
3212                                 Assert.Fail ("#A1");
3213                         } catch (ArgumentOutOfRangeException ex) {
3214                                 // Specified argument was out of the range of valid values
3215                                 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
3216                                 Assert.IsNull (ex.InnerException, "#A3");
3217                                 Assert.IsNotNull (ex.Message, "#A4");
3218                                 Assert.AreEqual ("size", ex.ParamName, "#A5");
3219                         } finally {
3220                                 s.Close ();
3221                         }
3222
3223                         // size > buffer length
3224                         s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
3225                                                         ProtocolType.Tcp);
3226                         try {
3227                                 s.ReceiveFrom (buffer, (buffer.Length + 1), (SocketFlags) 666,
3228                                         ref remoteEP);
3229                                 Assert.Fail ("#B1");
3230                         } catch (ArgumentOutOfRangeException ex) {
3231                                 // Specified argument was out of the range of valid values
3232                                 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#B2");
3233                                 Assert.IsNull (ex.InnerException, "#B3");
3234                                 Assert.IsNotNull (ex.Message, "#B4");
3235                                 Assert.AreEqual ("size", ex.ParamName, "#B5");
3236                         } finally {
3237                                 s.Close ();
3238                         }
3239                 }
3240
3241                 [Test] // ReceiveFrom (Byte [], Int32, SocketFlags, ref EndPoint)
3242                 public void ReceiveFrom3_Socket_Closed ()
3243                 {
3244                         Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
3245                                 ProtocolType.Tcp);
3246                         s.Close ();
3247
3248                         EndPoint remoteEP = new IPEndPoint (IPAddress.Loopback, 8001);
3249                         try {
3250                                 s.ReceiveFrom ((Byte []) null, -1, (SocketFlags) 666,
3251                                         ref remoteEP);
3252                                 Assert.Fail ("#1");
3253                         } catch (ObjectDisposedException ex) {
3254                                 // Cannot access a disposed object
3255                                 Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
3256                                 Assert.IsNull (ex.InnerException, "#3");
3257                                 Assert.IsNotNull (ex.Message, "#4");
3258                                 Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
3259                         }
3260                 }
3261
3262                 [Test] // ReceiveFrom (Byte [], Int32, Int32, SocketFlags, EndPoint)
3263                 public void ReceiveFrom4_Buffer_Null ()
3264                 {
3265                         Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
3266                                 ProtocolType.Tcp);
3267                         EndPoint remoteEP = new IPEndPoint (IPAddress.Loopback, 8001);
3268
3269                         try {
3270                                 s.ReceiveFrom ((Byte []) null, -1, -1, (SocketFlags) 666,
3271                                         ref remoteEP);
3272                                 Assert.Fail ("#1");
3273                         } catch (ArgumentNullException ex) {
3274                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
3275                                 Assert.IsNull (ex.InnerException, "#3");
3276                                 Assert.IsNotNull (ex.Message, "#4");
3277                                 Assert.AreEqual ("buffer", ex.ParamName, "#5");
3278                         }
3279                 }
3280
3281                 [Test] // ReceiveFrom (Byte [], Int32, Int32, SocketFlags, EndPoint)
3282                 public void ReceiveFrom4_Offset_OutOfRange ()
3283                 {
3284                         Socket s;
3285                         byte [] buffer = new byte [5];
3286                         EndPoint remoteEP = new IPEndPoint (IPAddress.Loopback, 8001);
3287
3288                         // offset negative
3289                         s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
3290                                                         ProtocolType.Tcp);
3291                         try {
3292                                 s.ReceiveFrom (buffer, -1, 0, (SocketFlags) 666,
3293                                         ref remoteEP);
3294                                 Assert.Fail ("#A1");
3295                         } catch (ArgumentOutOfRangeException ex) {
3296                                 // Specified argument was out of the range of valid values
3297                                 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
3298                                 Assert.IsNull (ex.InnerException, "#A3");
3299                                 Assert.IsNotNull (ex.Message, "#A4");
3300                                 Assert.AreEqual ("offset", ex.ParamName, "#A5");
3301                         } finally {
3302                                 s.Close ();
3303                         }
3304
3305                         // offset > buffer length
3306                         s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
3307                                                         ProtocolType.Tcp);
3308                         try {
3309                                 s.ReceiveFrom (buffer, (buffer.Length + 1), 0, (SocketFlags) 666,
3310                                         ref remoteEP);
3311                                 Assert.Fail ("#B1");
3312                         } catch (ArgumentOutOfRangeException ex) {
3313                                 // Specified argument was out of the range of valid values
3314                                 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#B2");
3315                                 Assert.IsNull (ex.InnerException, "#B3");
3316                                 Assert.IsNotNull (ex.Message, "#B4");
3317                                 Assert.AreEqual ("offset", ex.ParamName, "#B5");
3318                         } finally {
3319                                 s.Close ();
3320                         }
3321                 }
3322
3323                 [Test] // ReceiveFrom (Byte [], Int32, Int32, SocketFlags, ref IPEndPoint)
3324                 public void ReceiveFrom4_RemoteEP_Null ()
3325                 {
3326                         Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
3327                                 ProtocolType.Tcp);
3328                         byte [] buffer = new byte [5];
3329                         EndPoint remoteEP = null;
3330
3331                         try {
3332                                 s.ReceiveFrom (buffer, -1, -1, (SocketFlags) 666, ref remoteEP);
3333                                 Assert.Fail ("#1");
3334                         } catch (ArgumentNullException ex) {
3335                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
3336                                 Assert.IsNull (ex.InnerException, "#3");
3337                                 Assert.IsNotNull (ex.Message, "#4");
3338                                 Assert.AreEqual ("remoteEP", ex.ParamName, "#5");
3339                         } finally {
3340                                 s.Close ();
3341                         }
3342                 }
3343
3344                 [Test] // ReceiveFrom (Byte [], Int32, Int32, SocketFlags, EndPoint)
3345                 public void ReceiveFrom4_Size_OutOfRange ()
3346                 {
3347                         Socket s;
3348                         byte [] buffer = new byte [5];
3349                         EndPoint remoteEP = new IPEndPoint (IPAddress.Loopback, 8001);
3350
3351                         // size negative
3352                         s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
3353                                                         ProtocolType.Tcp);
3354                         try {
3355                                 s.ReceiveFrom (buffer, 0, -1, (SocketFlags) 666,
3356                                         ref remoteEP);
3357                                 Assert.Fail ("#A1");
3358                         } catch (ArgumentOutOfRangeException ex) {
3359                                 // Specified argument was out of the range of valid values
3360                                 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
3361                                 Assert.IsNull (ex.InnerException, "#A3");
3362                                 Assert.IsNotNull (ex.Message, "#A4");
3363                                 Assert.AreEqual ("size", ex.ParamName, "#A5");
3364                         } finally {
3365                                 s.Close ();
3366                         }
3367
3368                         // size > buffer length
3369                         s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
3370                                                         ProtocolType.Tcp);
3371                         try {
3372                                 s.ReceiveFrom (buffer, 0, (buffer.Length + 1), (SocketFlags) 666,
3373                                         ref remoteEP);
3374                                 Assert.Fail ("#B1");
3375                         } catch (ArgumentOutOfRangeException ex) {
3376                                 // Specified argument was out of the range of valid values
3377                                 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#B2");
3378                                 Assert.IsNull (ex.InnerException, "#B3");
3379                                 Assert.IsNotNull (ex.Message, "#B4");
3380                                 Assert.AreEqual ("size", ex.ParamName, "#B5");
3381                         } finally {
3382                                 s.Close ();
3383                         }
3384
3385                         // offset + size > buffer length
3386                         s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
3387                                                         ProtocolType.Tcp);
3388                         try {
3389                                 s.ReceiveFrom (buffer, 2, 4, (SocketFlags) 666, ref remoteEP);
3390                                 Assert.Fail ("#C1");
3391                         } catch (ArgumentOutOfRangeException ex) {
3392                                 // Specified argument was out of the range of valid values
3393                                 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#C2");
3394                                 Assert.IsNull (ex.InnerException, "#C3");
3395                                 Assert.IsNotNull (ex.Message, "#C4");
3396                                 Assert.AreEqual ("size", ex.ParamName, "#C5");
3397                         } finally {
3398                                 s.Close ();
3399                         }
3400                 }
3401
3402                 [Test] // ReceiveFrom (Byte [], Int32, Int32, SocketFlags, ref EndPoint)
3403                 public void ReceiveFrom4_Socket_Closed ()
3404                 {
3405                         Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
3406                                 ProtocolType.Tcp);
3407                         s.Close ();
3408
3409                         byte [] buffer = new byte [5];
3410                         EndPoint remoteEP = new IPEndPoint (IPAddress.Loopback, 8001);
3411                         try {
3412                                 s.ReceiveFrom (buffer, -1, -1, (SocketFlags) 666,
3413                                         ref remoteEP);
3414                                 Assert.Fail ("#1");
3415                         } catch (ObjectDisposedException ex) {
3416                                 // Cannot access a disposed object
3417                                 Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
3418                                 Assert.IsNull (ex.InnerException, "#3");
3419                                 Assert.IsNotNull (ex.Message, "#4");
3420                                 Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
3421                         }
3422                 }
3423
3424                 [Test]
3425                 public void ReceiveRemoteClosed ()
3426                 {
3427                         Socket sock = new Socket (AddressFamily.InterNetwork,
3428                                                   SocketType.Stream,
3429                                                   ProtocolType.Tcp);
3430                         sock.Bind (new IPEndPoint (IPAddress.Loopback, 1257));
3431                         sock.Listen (1);
3432                         
3433                         RRCReady.Reset ();
3434                         Thread client_thread = new Thread (new ThreadStart (RRCClientThread));
3435                         client_thread.Start ();
3436                         
3437                         Socket client = sock.Accept ();
3438                         NetworkStream stream = new NetworkStream (client);
3439                         stream.WriteByte (0x00);
3440                         stream.WriteByte (0x00);
3441                         client.Close ();
3442                         sock.Close ();
3443
3444                         RRCReady.WaitOne (1000, false);
3445                         Assert.IsTrue (RRCLastRead);
3446                 }
3447
3448                 //
3449                 // Test case for bug #471580
3450                 [Test]
3451                 public void UdpDoubleBind ()
3452                 {
3453                         Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
3454                         s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, 1);
3455                         
3456                         s.Bind (new IPEndPoint (IPAddress.Any, 12345));
3457                         
3458                         Socket ss = new Socket (AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
3459                         ss.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, 1);
3460                         
3461                         ss.Bind (new IPEndPoint (IPAddress.Any, 12345));
3462
3463                         // If we make it this far, we succeeded.
3464                         
3465                         ss.Close ();
3466                         s.Close ();
3467                 }
3468                 
3469 #if NET_2_0
3470                 [Test]
3471                 [Category ("NotOnMac")]
3472                 public void ConnectedProperty ()
3473                 {
3474                         TcpListener listener = new TcpListener (IPAddress.Loopback, 23456);
3475                         listener.Start();
3476
3477                         Socket client = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
3478                         client.Connect (IPAddress.Loopback, 23456);
3479                         Socket server = listener.AcceptSocket ();
3480
3481                         try {
3482                                 server.EndSend(server.BeginSend (new byte[10], 0, 10, SocketFlags.None, null, null));
3483                                 client.Close ();
3484                                 try {
3485                                         server.EndReceive (server.BeginReceive (new byte[10], 0, 10, SocketFlags.None, null, null));
3486                                 } catch {
3487                                 }
3488                                 Assert.IsTrue (!client.Connected);
3489                                 Assert.IsTrue (!server.Connected);
3490                         } finally {
3491                                 listener.Stop ();
3492                                 client.Close ();
3493                                 server.Close ();
3494                         }
3495                 }
3496 #endif
3497
3498                 [Test] // GetSocketOption (SocketOptionLevel, SocketOptionName)
3499                 public void GetSocketOption1_Socket_Closed ()
3500                 {
3501                         Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
3502                         s.Close ();
3503                         try {
3504                                 s.GetSocketOption (0, 0);
3505                                 Assert.Fail ("#1");
3506                         } catch (ObjectDisposedException ex) {
3507                                 // Cannot access a disposed object
3508                                 Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
3509                                 Assert.IsNull (ex.InnerException, "#3");
3510                                 Assert.IsNotNull (ex.Message, "#4");
3511                                 Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
3512                         }
3513                 }
3514
3515                 [Test] // GetSocketOption (SocketOptionLevel, SocketOptionName, Byte [])
3516                 public void GetSocketOption2_OptionValue_Null ()
3517                 {
3518                         Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
3519                         try {
3520                                 s.GetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Linger,
3521                                         (byte []) null);
3522                                 Assert.Fail ("#1");
3523                                 } catch (SocketException ex) {
3524                                         // The system detected an invalid pointer address in attempting
3525                                         // to use a pointer argument in a call
3526                                         Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
3527                                         Assert.AreEqual (10014, ex.ErrorCode, "#3");
3528                                         Assert.IsNull (ex.InnerException, "#4");
3529                                         Assert.IsNotNull (ex.Message, "#5");
3530                                         Assert.AreEqual (10014, ex.NativeErrorCode, "#6");
3531 #if NET_2_0
3532                                         Assert.AreEqual (SocketError.Fault, ex.SocketErrorCode, "#7");
3533 #endif
3534                                 }
3535                 }
3536
3537                 [Test] // GetSocketOption (SocketOptionLevel, SocketOptionName, Byte [])
3538                 public void GetSocketOption2_Socket_Closed ()
3539                 {
3540                         Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
3541                         s.Close ();
3542                         try {
3543                                 s.GetSocketOption (0, 0, (byte []) null);
3544                                 Assert.Fail ("#1");
3545                         } catch (ObjectDisposedException ex) {
3546                                 // Cannot access a disposed object
3547                                 Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
3548                                 Assert.IsNull (ex.InnerException, "#3");
3549                                 Assert.IsNotNull (ex.Message, "#4");
3550                                 Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
3551                         }
3552                 }
3553
3554                 [Test] // GetSocketOption (SocketOptionLevel, SocketOptionName, Int32)
3555                 public void GetSocketOption3_Socket_Closed ()
3556                 {
3557                         Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
3558                         s.Close ();
3559                         try {
3560                                 s.GetSocketOption (0, 0, 0);
3561                                 Assert.Fail ("#1");
3562                         } catch (ObjectDisposedException ex) {
3563                                 // Cannot access a disposed object
3564                                 Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
3565                                 Assert.IsNull (ex.InnerException, "#3");
3566                                 Assert.IsNotNull (ex.Message, "#4");
3567                                 Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
3568                         }
3569                 }
3570
3571                 [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Byte [])
3572                 public void SetSocketOption1_DontLinger ()
3573                 {
3574                         using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
3575                                 s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.DontLinger,
3576                                         new byte [] { 0x00 });
3577                                 s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.DontLinger,
3578                                         new byte [] { 0x01 });
3579                         }
3580                 }
3581
3582                 [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Byte [])
3583                 public void SetSocketOption1_DontLinger_Null ()
3584                 {
3585                         using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
3586                                 try {
3587                                         s.SetSocketOption (SocketOptionLevel.Socket,
3588                                                 SocketOptionName.DontLinger, (byte []) null);
3589                                         Assert.Fail ("#1");
3590                                 } catch (SocketException ex) {
3591                                         // The system detected an invalid pointer address in attempting
3592                                         // to use a pointer argument in a call
3593                                         Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
3594                                         Assert.AreEqual (10014, ex.ErrorCode, "#3");
3595                                         Assert.IsNull (ex.InnerException, "#4");
3596                                         Assert.IsNotNull (ex.Message, "#5");
3597                                         Assert.AreEqual (10014, ex.NativeErrorCode, "#6");
3598 #if NET_2_0
3599                                         Assert.AreEqual (SocketError.Fault, ex.SocketErrorCode, "#7");
3600 #endif
3601                                 }
3602                         }
3603                 }
3604
3605                 [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Byte [])
3606                 public void SetSocketOption1_Linger_Null ()
3607                 {
3608                         using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
3609                                 try {
3610                                         s.SetSocketOption (SocketOptionLevel.Socket,
3611                                                 SocketOptionName.DontLinger, (byte []) null);
3612                                         Assert.Fail ("#1");
3613                                 } catch (SocketException ex) {
3614                                         // The system detected an invalid pointer address in attempting
3615                                         // to use a pointer argument in a call
3616                                         Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
3617                                         Assert.AreEqual (10014, ex.ErrorCode, "#3");
3618                                         Assert.IsNull (ex.InnerException, "#4");
3619                                         Assert.IsNotNull (ex.Message, "#5");
3620                                         Assert.AreEqual (10014, ex.NativeErrorCode, "#6");
3621 #if NET_2_0
3622                                         Assert.AreEqual (SocketError.Fault, ex.SocketErrorCode, "#7");
3623 #endif
3624                                 }
3625                         }
3626                 }
3627
3628                 [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Byte [])
3629                 public void SetSocketOption1_Socket_Close ()
3630                 {
3631                         Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
3632                         s.Close ();
3633                         try {
3634                                 s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.DontLinger,
3635                                         new byte [] { 0x00 });
3636                                 Assert.Fail ("#1");
3637                         } catch (ObjectDisposedException ex) {
3638                                 Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
3639                                 Assert.IsNull (ex.InnerException, "#3");
3640                                 Assert.IsNotNull (ex.Message, "#4");
3641                                 Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
3642                         }
3643                 }
3644
3645                 [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Int32)
3646                 public void SetSocketOption2_DontLinger ()
3647                 {
3648                         using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
3649                                 s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.DontLinger, 0);
3650                                 s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.DontLinger, 5);
3651                         }
3652                 }
3653
3654                 [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Int32)
3655                 [Category ("NotWorking")]
3656                 public void SetSocketOption2_Linger ()
3657                 {
3658                         using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
3659                                 s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Linger, 0);
3660                                 s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Linger, 5);
3661                         }
3662                 }
3663
3664                 [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Int32)
3665                 public void SetSocketOption2_Socket_Closed ()
3666                 {
3667                         Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
3668                         s.Close ();
3669                         try {
3670                                 s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.DontLinger, 0);
3671                                 Assert.Fail ("#1");
3672                         } catch (ObjectDisposedException ex) {
3673                                 // Cannot access a disposed object
3674                                 Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
3675                                 Assert.IsNull (ex.InnerException, "#3");
3676                                 Assert.IsNotNull (ex.Message, "#4");
3677                                 Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
3678                         }
3679                 }
3680
3681                 [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
3682                 public void SetSocketOption3_AddMembershipIPv4_IPv6MulticastOption ()
3683                 {
3684                         IPAddress mcast_addr = IPAddress.Parse ("239.255.255.250");
3685
3686                         using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)) {
3687                                 s.Bind (new IPEndPoint (IPAddress.Any, 1901));
3688                                 try {
3689                                         s.SetSocketOption (SocketOptionLevel.IP, SocketOptionName.AddMembership,
3690                                                 new IPv6MulticastOption (mcast_addr));
3691                                         Assert.Fail ("#1");
3692                                 } catch (ArgumentException ex) {
3693                                         Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
3694                                         Assert.IsNull (ex.InnerException, "#3");
3695                                         Assert.IsNotNull (ex.Message, "#4");
3696 #if NET_2_0
3697                                         // The specified value is not a valid 'MulticastOption'
3698                                         Assert.IsTrue (ex.Message.IndexOf ("'MulticastOption'") != -1, "#5:" + ex.Message);
3699                                         Assert.AreEqual ("optionValue", ex.ParamName, "#6");
3700 #else
3701                                         Assert.AreEqual ("optionValue", ex.Message, "#5");
3702                                         Assert.IsNull (ex.ParamName, "#6");
3703 #endif
3704                                 }
3705                         }
3706                 }
3707
3708                 [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
3709                 public void SetSocketOption3_AddMembershipIPv4_MulticastOption ()
3710                 {
3711                         IPAddress mcast_addr = IPAddress.Parse ("239.255.255.250");
3712
3713                         using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)) {
3714                                 s.Bind (new IPEndPoint (IPAddress.Any, 1901));
3715                                 s.SetSocketOption (SocketOptionLevel.IP, SocketOptionName.AddMembership,
3716                                         new MulticastOption (mcast_addr));
3717                         }
3718                 }
3719
3720                 [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
3721                 [Category ("NotWorking")]
3722                 public void SetSocketOption3_AddMembershipIPv4_Socket_NotBound ()
3723                 {
3724                         IPAddress mcast_addr = IPAddress.Parse ("239.255.255.250");
3725
3726                         Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
3727                         try {
3728                                 s.SetSocketOption (SocketOptionLevel.IP, SocketOptionName.AddMembership,
3729                                         new MulticastOption (mcast_addr));
3730                                 Assert.Fail ("#1");
3731                         } catch (SocketException ex) {
3732                                 // An invalid argument was supplied
3733                                 Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
3734                                 Assert.AreEqual (10022, ex.ErrorCode, "#3");
3735                                 Assert.IsNull (ex.InnerException, "#4");
3736                                 Assert.IsNotNull (ex.Message, "#5");
3737                                 Assert.AreEqual (10022, ex.NativeErrorCode, "#6");
3738 #if NET_2_0
3739                                 Assert.AreEqual (SocketError.InvalidArgument, ex.SocketErrorCode, "#7");
3740 #endif
3741                         } finally {
3742                                 s.Close ();
3743                         }
3744                 }
3745
3746                 [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
3747                 public void SetSocketOption3_AddMembershipIPv6_IPv6MulticastOption ()
3748                 {
3749 #if NET_2_0
3750                         if (!Socket.OSSupportsIPv6)
3751 #else
3752                         if (!Socket.SupportsIPv6)
3753 #endif
3754                                 Assert.Ignore ("IPv6 not enabled.");
3755
3756                         IPAddress mcast_addr = IPAddress.Parse ("ff02::1");
3757
3758                         using (Socket s = new Socket (AddressFamily.InterNetworkV6, SocketType.Dgram, ProtocolType.Udp)) {
3759                                 s.Bind (new IPEndPoint (IPAddress.IPv6Any, 1902));
3760                                 s.SetSocketOption (SocketOptionLevel.IPv6, SocketOptionName.AddMembership,
3761                                         new IPv6MulticastOption (mcast_addr));
3762                         }
3763                 }
3764
3765                 [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
3766                 public void SetSocketOption3_AddMembershipIPv6_MulticastOption ()
3767                 {
3768 #if NET_2_0
3769                         if (!Socket.OSSupportsIPv6)
3770 #else
3771                         if (!Socket.SupportsIPv6)
3772 #endif
3773                                 Assert.Ignore ("IPv6 not enabled.");
3774
3775                         IPAddress mcast_addr = IPAddress.Parse ("ff02::1");
3776
3777                         using (Socket s = new Socket (AddressFamily.InterNetworkV6, SocketType.Dgram, ProtocolType.Udp)) {
3778                                 s.Bind (new IPEndPoint (IPAddress.IPv6Any, 1902));
3779                                 try {
3780                                         s.SetSocketOption (SocketOptionLevel.IPv6, SocketOptionName.AddMembership,
3781                                                 new MulticastOption (mcast_addr));
3782                                         Assert.Fail ("#1");
3783                                 } catch (ArgumentException ex) {
3784                                         Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
3785                                         Assert.IsNull (ex.InnerException, "#3");
3786                                         Assert.IsNotNull (ex.Message, "#4");
3787 #if NET_2_0
3788                                         // The specified value is not a valid 'IPv6MulticastOption'
3789                                         Assert.IsTrue (ex.Message.IndexOf ("'IPv6MulticastOption'") != -1, "#5:" + ex.Message);
3790                                         Assert.AreEqual ("optionValue", ex.ParamName, "#6");
3791 #else
3792                                         Assert.AreEqual ("optionValue", ex.Message, "#5");
3793                                         Assert.IsNull (ex.ParamName, "#6");
3794 #endif
3795                                 }
3796                         }
3797                 }
3798
3799                 [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
3800                 [Category ("NotWorking")]
3801                 public void SetSocketOption3_AddMembershipIPv6_Socket_NotBound ()
3802                 {
3803                         IPAddress mcast_addr = IPAddress.Parse ("ff02::1");
3804
3805                         Socket s = new Socket (AddressFamily.InterNetworkV6, SocketType.Dgram, ProtocolType.Udp);
3806                         try {
3807                                 s.SetSocketOption (SocketOptionLevel.IPv6, SocketOptionName.AddMembership,
3808                                         new IPv6MulticastOption (mcast_addr));
3809                                 Assert.Fail ("#1");
3810                         } catch (SocketException ex) {
3811                                 // An invalid argument was supplied
3812                                 Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
3813                                 Assert.AreEqual (10022, ex.ErrorCode, "#3");
3814                                 Assert.IsNull (ex.InnerException, "#4");
3815                                 Assert.IsNotNull (ex.Message, "#5");
3816                                 Assert.AreEqual (10022, ex.NativeErrorCode, "#6");
3817 #if NET_2_0
3818                                 Assert.AreEqual (SocketError.InvalidArgument, ex.SocketErrorCode, "#7");
3819 #endif
3820                         } finally {
3821                                 s.Close ();
3822                         }
3823                 }
3824
3825                 [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
3826                 public void SetSocketOption3_DontLinger_Boolean ()
3827                 {
3828                         using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
3829                                 try {
3830                                         s.SetSocketOption (SocketOptionLevel.Socket,
3831                                                 SocketOptionName.DontLinger, (object) false);
3832                                         Assert.Fail ("#1");
3833                                 } catch (ArgumentException ex) {
3834                                         // The specified value is not valid
3835                                         Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
3836                                         Assert.IsNull (ex.InnerException, "#3");
3837 #if NET_2_0
3838                                         Assert.IsNotNull (ex.Message, "#4");
3839                                         Assert.AreEqual ("optionValue", ex.ParamName, "#5");
3840 #else
3841                                         Assert.AreEqual ("optionValue", ex.Message, "#4");
3842                                         Assert.IsNull (ex.ParamName, "#5");
3843 #endif
3844                                 }
3845                         }
3846                 }
3847
3848                 [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
3849                 public void SetSocketOption3_DontLinger_Int32 ()
3850                 {
3851                         using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
3852                                 try {
3853                                         s.SetSocketOption (SocketOptionLevel.Socket,
3854                                                 SocketOptionName.DontLinger, (object) 0);
3855                                         Assert.Fail ("#1");
3856                                 } catch (ArgumentException ex) {
3857                                         // The specified value is not valid
3858                                         Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
3859                                         Assert.IsNull (ex.InnerException, "#3");
3860 #if NET_2_0
3861                                         Assert.IsNotNull (ex.Message, "#4");
3862                                         Assert.AreEqual ("optionValue", ex.ParamName, "#5");
3863 #else
3864                                         Assert.AreEqual ("optionValue", ex.Message, "#4");
3865                                         Assert.IsNull (ex.ParamName, "#5");
3866 #endif
3867                                 }
3868                         }
3869                 }
3870
3871                 [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
3872                 public void SetSocketOption3_DontLinger_LingerOption ()
3873                 {
3874                         using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
3875                                 try {
3876                                         s.SetSocketOption (SocketOptionLevel.Socket,
3877                                                 SocketOptionName.DontLinger, new LingerOption (true, 1000));
3878                                         Assert.Fail ("#1");
3879                                 } catch (ArgumentException ex) {
3880                                         Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
3881                                         Assert.IsNull (ex.InnerException, "#3");
3882 #if NET_2_0
3883                                         // The specified value is not valid
3884                                         Assert.IsNotNull (ex.Message, "#4");
3885                                         Assert.AreEqual ("optionValue", ex.ParamName, "#5");
3886 #else
3887                                         Assert.AreEqual ("optionValue", ex.Message, "#4");
3888                                         Assert.IsNull (ex.ParamName, "#5");
3889 #endif
3890                                 }
3891                         }
3892                 }
3893
3894                 [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
3895                 public void SetSocketOption3_Linger_Boolean ()
3896                 {
3897                         using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
3898                                 try {
3899                                         s.SetSocketOption (SocketOptionLevel.Socket,
3900                                                 SocketOptionName.Linger, (object) false);
3901                                         Assert.Fail ("#1");
3902                                 } catch (ArgumentException ex) {
3903                                         Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
3904                                         Assert.IsNull (ex.InnerException, "#3");
3905 #if NET_2_0
3906                                         // The specified value is not valid
3907                                         Assert.IsNotNull (ex.Message, "#4");
3908                                         Assert.AreEqual ("optionValue", ex.ParamName, "#5");
3909 #else
3910                                         Assert.AreEqual ("optionValue", ex.Message, "#4");
3911                                         Assert.IsNull (ex.ParamName, "#5");
3912 #endif
3913                                 }
3914                         }
3915                 }
3916
3917                 [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
3918                 public void SetSocketOption3_Linger_Int32 ()
3919                 {
3920                         using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
3921                                 try {
3922                                         s.SetSocketOption (SocketOptionLevel.Socket,
3923                                                 SocketOptionName.Linger, (object) 0);
3924                                         Assert.Fail ("#1");
3925                                 } catch (ArgumentException ex) {
3926                                         Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
3927                                         Assert.IsNull (ex.InnerException, "#3");
3928 #if NET_2_0
3929                                         // The specified value is not valid
3930                                         Assert.IsNotNull (ex.Message, "#4");
3931                                         Assert.AreEqual ("optionValue", ex.ParamName, "#5");
3932 #else
3933                                         Assert.AreEqual ("optionValue", ex.Message, "#4");
3934                                         Assert.IsNull (ex.ParamName, "#5");
3935 #endif
3936                                 }
3937                         }
3938                 }
3939
3940                 [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
3941                 public void SetSocketOption3_Linger_LingerOption ()
3942                 {
3943                         using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
3944                                 s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Linger,
3945                                         new LingerOption (false, 0));
3946                                 s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Linger,
3947                                         new LingerOption (true, 0));
3948                                 s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Linger,
3949                                         new LingerOption (false, 1000));
3950                                 s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Linger,
3951                                         new LingerOption (true, 1000));
3952                         }
3953                 }
3954
3955                 [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
3956                 public void SetSocketOption3_DropMembershipIPv4_IPv6MulticastOption ()
3957                 {
3958                         IPAddress mcast_addr = IPAddress.Parse ("239.255.255.250");
3959
3960                         using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)) {
3961                                 s.Bind (new IPEndPoint (IPAddress.Any, 1901));
3962                                 s.SetSocketOption (SocketOptionLevel.IP, SocketOptionName.AddMembership,
3963                                         new MulticastOption (mcast_addr));
3964                                 try {
3965                                         s.SetSocketOption (SocketOptionLevel.IP, SocketOptionName.DropMembership,
3966                                                 new IPv6MulticastOption (mcast_addr));
3967                                         Assert.Fail ("#1");
3968                                 } catch (ArgumentException ex) {
3969                                         Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
3970                                         Assert.IsNull (ex.InnerException, "#3");
3971                                         Assert.IsNotNull (ex.Message, "#4");
3972 #if NET_2_0
3973                                         // The specified value is not a valid 'MulticastOption'
3974                                         Assert.IsTrue (ex.Message.IndexOf ("'MulticastOption'") != -1, "#5:" + ex.Message);
3975                                         Assert.AreEqual ("optionValue", ex.ParamName, "#6");
3976 #else
3977                                         Assert.AreEqual ("optionValue", ex.Message, "#5");
3978                                         Assert.IsNull (ex.ParamName, "#6");
3979 #endif
3980                                 }
3981                         }
3982                 }
3983
3984                 [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
3985                 public void SetSocketOption3_DropMembershipIPv4_MulticastOption ()
3986                 {
3987                         IPAddress mcast_addr = IPAddress.Parse ("239.255.255.250");
3988
3989                         using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)) {
3990                                 MulticastOption option = new MulticastOption (mcast_addr);
3991
3992                                 s.Bind (new IPEndPoint (IPAddress.Any, 1901));
3993                                 s.SetSocketOption (SocketOptionLevel.IP, SocketOptionName.AddMembership,
3994                                         option);
3995                                 s.SetSocketOption (SocketOptionLevel.IP, SocketOptionName.DropMembership,
3996                                         option);
3997                         }
3998                 }
3999
4000                 [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
4001                 [Category ("NotWorking")]
4002                 public void SetSocketOption3_DropMembershipIPv4_Socket_NotBound ()
4003                 {
4004                         IPAddress mcast_addr = IPAddress.Parse ("239.255.255.250");
4005
4006                         Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
4007                         try {
4008                                 s.SetSocketOption (SocketOptionLevel.IP, SocketOptionName.DropMembership,
4009                                         new MulticastOption (mcast_addr));
4010                                 Assert.Fail ("#1");
4011                         } catch (SocketException ex) {
4012                                 // An invalid argument was supplied
4013                                 Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
4014                                 Assert.AreEqual (10022, ex.ErrorCode, "#3");
4015                                 Assert.IsNull (ex.InnerException, "#4");
4016                                 Assert.IsNotNull (ex.Message, "#5");
4017                                 Assert.AreEqual (10022, ex.NativeErrorCode, "#6");
4018 #if NET_2_0
4019                                 Assert.AreEqual (SocketError.InvalidArgument, ex.SocketErrorCode, "#7");
4020 #endif
4021                         } finally {
4022                                 s.Close ();
4023                         }
4024                 }
4025
4026                 [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
4027                 public void SetSocketOption3_DropMembershipIPv6_IPv6MulticastOption ()
4028                 {
4029 #if NET_2_0
4030                         if (!Socket.OSSupportsIPv6)
4031 #else
4032                         if (!Socket.SupportsIPv6)
4033 #endif
4034                                 Assert.Ignore ("IPv6 not enabled.");
4035
4036                         using (Socket s = new Socket (AddressFamily.InterNetworkV6, SocketType.Dgram, ProtocolType.Udp)) {
4037                                 IPv6MulticastOption option = new IPv6MulticastOption (
4038                                         IPAddress.Parse ("ff02::1"));
4039
4040                                 s.Bind (new IPEndPoint (IPAddress.IPv6Any, 1902));
4041                                 s.SetSocketOption (SocketOptionLevel.IPv6, SocketOptionName.AddMembership,
4042                                         option);
4043                                 s.SetSocketOption (SocketOptionLevel.IPv6, SocketOptionName.DropMembership,
4044                                         option);
4045                         }
4046                 }
4047
4048                 [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
4049                 public void SetSocketOption3_DropMembershipIPv6_MulticastOption ()
4050                 {
4051 #if NET_2_0
4052                         if (!Socket.OSSupportsIPv6)
4053 #else
4054                         if (!Socket.SupportsIPv6)
4055 #endif
4056                                 Assert.Ignore ("IPv6 not enabled.");
4057
4058                         IPAddress mcast_addr = IPAddress.Parse ("ff02::1");
4059
4060                         using (Socket s = new Socket (AddressFamily.InterNetworkV6, SocketType.Dgram, ProtocolType.Udp)) {
4061                                 s.Bind (new IPEndPoint (IPAddress.IPv6Any, 1902));
4062                                 s.SetSocketOption (SocketOptionLevel.IPv6, SocketOptionName.AddMembership,
4063                                         new IPv6MulticastOption (mcast_addr));
4064                                 try {
4065                                         s.SetSocketOption (SocketOptionLevel.IPv6, SocketOptionName.DropMembership,
4066                                                 new MulticastOption (mcast_addr));
4067                                         Assert.Fail ("#1");
4068                                 } catch (ArgumentException ex) {
4069                                         Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
4070                                         Assert.IsNull (ex.InnerException, "#3");
4071                                         Assert.IsNotNull (ex.Message, "#4");
4072 #if NET_2_0
4073                                         // The specified value is not a valid 'IPv6MulticastOption'
4074                                         Assert.IsTrue (ex.Message.IndexOf ("'IPv6MulticastOption'") != -1, "#5:" + ex.Message);
4075                                         Assert.AreEqual ("optionValue", ex.ParamName, "#6");
4076 #else
4077                                         Assert.AreEqual ("optionValue", ex.Message, "#5");
4078                                         Assert.IsNull (ex.ParamName, "#6");
4079 #endif
4080                                 }
4081                         }
4082                 }
4083
4084                 [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
4085                 [Category ("NotWorking")]
4086                 public void SetSocketOption3_DropMembershipIPv6_Socket_NotBound ()
4087                 {
4088                         IPAddress mcast_addr = IPAddress.Parse ("ff02::1");
4089
4090                         Socket s = new Socket (AddressFamily.InterNetworkV6, SocketType.Dgram, ProtocolType.Udp);
4091                         try {
4092                                 s.SetSocketOption (SocketOptionLevel.IPv6, SocketOptionName.DropMembership,
4093                                         new IPv6MulticastOption (mcast_addr));
4094                                 Assert.Fail ("#1");
4095                         } catch (SocketException ex) {
4096                                 // An invalid argument was supplied
4097                                 Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
4098                                 Assert.AreEqual (10022, ex.ErrorCode, "#3");
4099                                 Assert.IsNull (ex.InnerException, "#4");
4100                                 Assert.IsNotNull (ex.Message, "#5");
4101                                 Assert.AreEqual (10022, ex.NativeErrorCode, "#6");
4102 #if NET_2_0
4103                                 Assert.AreEqual (SocketError.InvalidArgument, ex.SocketErrorCode, "#7");
4104 #endif
4105                         } finally {
4106                                 s.Close ();
4107                         }
4108                 }
4109
4110                 [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
4111                 public void SetSocketOption3_OptionValue_Null ()
4112                 {
4113                         using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
4114                                 try {
4115                                         s.SetSocketOption (SocketOptionLevel.Socket,
4116                                                 SocketOptionName.Linger, (object) null);
4117                                         Assert.Fail ("#1");
4118                                 } catch (ArgumentNullException ex) {
4119                                         Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
4120                                         Assert.IsNull (ex.InnerException, "#3");
4121                                         Assert.IsNotNull (ex.Message, "#4");
4122                                         Assert.AreEqual ("optionValue", ex.ParamName, "#5");
4123                                 }
4124                         }
4125                 }
4126
4127                 [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
4128                 public void SetSocketOption3_Socket_Closed ()
4129                 {
4130                         Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
4131                         s.Close ();
4132                         try {
4133                                 s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Linger,
4134                                         new LingerOption (false, 0));
4135                                 Assert.Fail ("#1");
4136                         } catch (ObjectDisposedException ex) {
4137                                 // Cannot access a disposed object
4138                                 Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
4139                                 Assert.IsNull (ex.InnerException, "#3");
4140                                 Assert.IsNotNull (ex.Message, "#4");
4141                                 Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
4142                         }
4143                 }
4144
4145                 [Test]
4146                 public void Shutdown_NoConnect ()
4147                 {
4148                         Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
4149                         s.Bind (new IPEndPoint (IPAddress.Loopback, 0));
4150                         s.Listen (1);
4151                         try {
4152                                 s.Shutdown (SocketShutdown.Both);
4153                                 Assert.Fail ("#1");
4154                         } catch (SocketException exc) {
4155                                 Assert.AreEqual (10057, exc.ErrorCode, "#2");
4156                         } finally {
4157                                 s.Close ();
4158                         }
4159                 }
4160
4161                 [Test]
4162                 [ExpectedException (typeof (NullReferenceException))]
4163                 public void ReceiveAsync_Null ()
4164                 {
4165                         using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
4166                                 s.ReceiveAsync (null);
4167                         }
4168                 }
4169
4170                 [Test]
4171                 [ExpectedException (typeof (NullReferenceException))]
4172                 public void ReceiveAsync_Default ()
4173                 {
4174                         using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
4175                                 SocketAsyncEventArgs saea = new SocketAsyncEventArgs ();
4176                                 s.ReceiveAsync (saea);
4177                         }
4178                 }
4179
4180
4181                 [Test]
4182                 [ExpectedException (typeof (NullReferenceException))]
4183                 public void ReceiveAsync_NullBuffer ()
4184                 {
4185                         using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
4186                                 SocketAsyncEventArgs saea = new SocketAsyncEventArgs ();
4187                                 saea.SetBuffer (null, 0, 0);
4188                                 s.ReceiveAsync (null);
4189                         }
4190                 }
4191
4192                 [Test]
4193                 [ExpectedException (typeof (ObjectDisposedException))]
4194                 public void ReceiveAsync_ClosedSocket ()
4195                 {
4196                         Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
4197                         s.Close ();
4198                         s.ReceiveAsync (null);
4199                 }
4200
4201                 [Test]
4202                 [ExpectedException (typeof (NullReferenceException))]
4203                 public void SendAsync_Null ()
4204                 {
4205                         using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
4206                                 s.SendAsync (null);
4207                         }
4208                 }
4209
4210                 [Test]
4211                 [ExpectedException (typeof (NullReferenceException))]
4212                 public void SendAsync_Default ()
4213                 {
4214                         using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
4215                                 SocketAsyncEventArgs saea = new SocketAsyncEventArgs ();
4216                                 s.SendAsync (saea);
4217                         }
4218                 }
4219
4220
4221                 [Test]
4222                 [ExpectedException (typeof (NullReferenceException))]
4223                 public void SendAsync_NullBuffer ()
4224                 {
4225                         using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
4226                                 SocketAsyncEventArgs saea = new SocketAsyncEventArgs ();
4227                                 saea.SetBuffer (null, 0, 0);
4228                                 s.SendAsync (null);
4229                         }
4230                 }
4231
4232                 [Test]
4233                 [ExpectedException (typeof (ObjectDisposedException))]
4234                 public void SendAsync_ClosedSocket ()
4235                 {
4236                         Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
4237                         s.Close ();
4238                         s.SendAsync (null);
4239                 }
4240         }
4241 }
4242