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