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