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