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