2009-06-12 Bill Holmes <billholmes54@gmail.com>
[mono.git] / mcs / class / System / Test / System.Net.Sockets / SocketTest.cs
1 // System.Net.Sockets.SocketTest.cs
2 //
3 // Authors:
4 //    Brad Fitzpatrick (brad@danga.com)
5 //    Gonzalo Paniagua Javier (gonzalo@novell.com)
6 //
7 // (C) Copyright 2003 Brad Fitzpatrick
8 // Copyright (c) 2005 Novell, Inc. (http://www.novell.com)
9 //
10
11 using System;
12 using System.Collections;
13 using System.Threading;
14 using System.Net;
15 using System.Net.Sockets;
16 using NUnit.Framework;
17
18 #if NET_2_0
19 using System.Collections.Generic;
20 #endif
21
22 namespace MonoTests.System.Net.Sockets
23 {
24         [TestFixture]
25         public class SocketTest
26         {
27                 // note: also used in SocketCas tests
28                 public const string BogusAddress = "192.168.244.244";
29                 public const int BogusPort = 23483;
30
31                 [Test]
32                 public void ConnectIPAddressAny ()
33                 {
34                         IPEndPoint ep = new IPEndPoint (IPAddress.Any, 0);
35
36                         try {
37                                 using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)) {
38                                         s.Connect (ep);
39                                         s.Close ();
40                                 }
41                                 Assert.Fail ("#1");
42                         } catch (SocketException ex) {
43                                 Assert.AreEqual (10049, ex.ErrorCode, "#2");
44                         }
45
46                         try {
47                                 using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
48                                         s.Connect (ep);
49                                         s.Close ();
50                                 }
51                                 Assert.Fail ("#3");
52                         } catch (SocketException ex) {
53                                 Assert.AreEqual (10049, ex.ErrorCode, "#4");
54                         }
55                 }
56
57                 [Test]
58                 [Ignore ("Bug #75158")]
59                 public void IncompatibleAddress ()
60                 {
61                         IPEndPoint epIPv6 = new IPEndPoint (IPAddress.IPv6Any,
62                                                                 0);
63
64                         try {
65                                 using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP)) {
66                                         s.Connect (epIPv6);
67                                         s.Close ();
68                                 }
69                                 Assert.Fail ("#1");
70                         } catch (SocketException ex) {
71 #if !NET_2_0
72                                 // invalid argument
73                                 int expectedError = 10022;
74 #else
75                                 // address incompatible with protocol
76                                 int expectedError = 10047;
77 #endif
78                                 Assert.AreEqual (expectedError, ex.ErrorCode,
79                                                 "#2");
80                         }
81                 }
82
83                 [Test]
84                 [Category ("InetAccess")]
85                 public void EndConnect ()
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                                 Assert.AreEqual (10060, ex.ErrorCode, "#2");
98                         }
99                 }
100
101                 [Test]
102                 [ExpectedException (typeof (ArgumentNullException))]
103                 public void SelectEmpty ()
104                 {
105                         ArrayList list = new ArrayList ();
106                         Socket.Select (list, list, list, 1000);
107                 }
108                 
109                 private bool BlockingConnect (bool block)
110                 {
111                         IPEndPoint ep = new IPEndPoint(IPAddress.Loopback, 1234);
112                         Socket server = new Socket(AddressFamily.InterNetwork,
113                                                    SocketType.Stream,
114                                                    ProtocolType.Tcp);
115                         server.Bind(ep);
116                         server.Blocking=block;
117
118                         server.Listen(0);
119
120                         Socket conn = new Socket (AddressFamily.InterNetwork,
121                                                   SocketType.Stream,
122                                                   ProtocolType.Tcp);
123                         conn.Connect (ep);
124
125                         Socket client = server.Accept();
126                         bool client_block = client.Blocking;
127
128                         client.Close();
129                         conn.Close();
130                         server.Close();
131                         
132                         return(client_block);
133                 }
134
135                 [Test]
136                 public void AcceptBlockingStatus()
137                 {
138                         bool block;
139
140                         block = BlockingConnect(true);
141                         Assertion.AssertEquals ("BlockingStatus01",
142                                                 block, true);
143
144                         block = BlockingConnect(false);
145                         Assertion.AssertEquals ("BlockingStatus02",
146                                                 block, false);
147                 }
148
149                 static bool CFAConnected = false;
150                 static ManualResetEvent CFACalledBack;
151                 
152                 private static void CFACallback (IAsyncResult asyncResult)
153                 {
154                         Socket sock = (Socket)asyncResult.AsyncState;
155                         CFAConnected = sock.Connected;
156                         
157                         if (sock.Connected) {
158                                 sock.EndConnect (asyncResult);
159                         }
160
161                         CFACalledBack.Set ();
162                 }
163
164                 [Test]
165                 public void ConnectFailAsync ()
166                 {
167                         Socket sock = new Socket (AddressFamily.InterNetwork,
168                                                   SocketType.Stream,
169                                                   ProtocolType.Tcp);
170                         sock.Blocking = false;
171                         CFACalledBack = new ManualResetEvent (false);
172                         CFACalledBack.Reset ();
173
174                         /* Need a port that is not being used for
175                          * anything...
176                          */
177                         sock.BeginConnect (new IPEndPoint (IPAddress.Loopback,
178                                                            114),
179                                            new AsyncCallback (CFACallback),
180                                            sock);
181                         CFACalledBack.WaitOne ();
182
183                         Assertion.AssertEquals ("ConnectFail", CFAConnected,
184                                                 false);
185                 }
186                 
187 #if !TARGET_JVM
188                 [Test]
189 #if !NET_2_0
190                 [ExpectedException (typeof (ArgumentException))]
191 #endif
192                 public void SetSocketOptionBoolean ()
193                 {
194                         IPEndPoint ep = new IPEndPoint (IPAddress.Loopback, 1);
195                         Socket sock = new Socket (ep.Address.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
196                         try {
197                                 sock.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);
198                         } finally {
199                                 sock.Close ();
200                         }
201                 }
202 #endif
203                 [Test]
204                 public void TestSelect1 ()
205                 {
206                         Socket srv = CreateServer ();
207                         ClientSocket clnt = new ClientSocket (srv.LocalEndPoint);
208                         Thread th = new Thread (new ThreadStart (clnt.ConnectSleepClose));
209                         Socket acc = null;
210                         try {
211                                 th.Start ();
212                                 acc = srv.Accept ();
213                                 clnt.Write ();
214                                 ArrayList list = new ArrayList ();
215                                 ArrayList empty = new ArrayList ();
216                                 list.Add (acc);
217                                 Socket.Select (list, empty, empty, 100);
218                                 Assertion.AssertEquals ("#01", 0, empty.Count);
219                                 Assertion.AssertEquals ("#02", 1, list.Count);
220                                 Socket.Select (empty, list, empty, 100);
221                                 Assertion.AssertEquals ("#03", 0, empty.Count);
222                                 Assertion.AssertEquals ("#04", 1, list.Count);
223                                 Socket.Select (list, empty, empty, -1);
224                                 Assertion.AssertEquals ("#05", 0, empty.Count);
225                                 Assertion.AssertEquals ("#06", 1, list.Count);
226                                 // Need to read the 10 bytes from the client to avoid a RST
227                                 byte [] bytes = new byte [10];
228                                 acc.Receive (bytes);
229                         } finally {
230                                 if (acc != null)
231                                         acc.Close ();
232                                 srv.Close ();
233                         }
234                 }
235
236                 static Socket CreateServer ()
237                 {
238                         Socket sock = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
239                         sock.Bind (new IPEndPoint (IPAddress.Loopback, 0));
240                         sock.Listen (1);
241                         return sock;
242                 }
243
244                 class ClientSocket {
245                         Socket sock;
246                         EndPoint ep;
247
248                         public ClientSocket (EndPoint ep)
249                         {
250                                 this.ep = ep;
251                                 sock = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
252                         }
253
254                         public void ConnectSleepClose ()
255                         {
256                                 sock.Connect (ep);
257                                 Thread.Sleep (2000);
258                                 sock.Close ();
259                         }
260
261                         public void Write ()
262                         {
263                                 byte [] b = new byte [10];
264                                 sock.Send (b);
265                         }
266                 }
267
268                 byte[] buf = new byte[100];
269
270                 [Test]
271                 [ExpectedException (typeof (ObjectDisposedException))]
272                 public void Disposed1 ()
273                 {
274                         Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
275                         EndPoint ep = new IPEndPoint (IPAddress.Any, 31337);
276                         s.Close();
277
278                         s.ReceiveFrom (buf, ref ep);
279                 }
280
281                 [Test]
282                 [ExpectedException (typeof (ObjectDisposedException))]
283                 public void Disposed2 ()
284                 {
285                         Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
286                         s.Close();
287
288                         s.Blocking = true;
289                 }
290
291                 [Test]
292                 [ExpectedException (typeof (ObjectDisposedException))]
293                 public void Disposed3 ()
294                 {
295                         Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
296                         s.Close();
297
298                         s.GetSocketOption (0, 0);
299                 }
300
301                 [Test]
302                 [ExpectedException (typeof (ObjectDisposedException))]
303                 public void Disposed4 ()
304                 {
305                         Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
306                         s.Close();
307
308                         s.GetSocketOption (0, 0, null);
309                 }
310
311                 [Test]
312                 [ExpectedException (typeof (ObjectDisposedException))]
313                 public void Disposed5 ()
314                 {
315                         Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
316                         s.Close();
317
318                         s.GetSocketOption (0, 0, 0);
319                 }
320
321                 [Test]
322                 [ExpectedException (typeof (ObjectDisposedException))]
323                 public void Disposed6 ()
324                 {
325                         Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
326                         s.Close();
327
328                         s.Listen (5);
329                 }
330
331                 [Test]
332                 [ExpectedException (typeof (ObjectDisposedException))]
333                 public void Disposed7 ()
334                 {
335                         Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
336                         s.Close();
337
338                         s.Poll (100, 0);
339                 }
340
341                 [Test]
342                 [ExpectedException (typeof (ObjectDisposedException))]
343                 public void Disposed8 ()
344                 {
345                         Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
346                         s.Close();
347
348                         s.Receive (buf);
349                 }
350
351                 [Test]
352                 [ExpectedException (typeof (ObjectDisposedException))]
353                 public void Disposed9 ()
354                 {
355                         Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
356                         s.Close();
357
358                         s.Receive (buf, 0);
359                 }
360
361                 [Test]
362                 [ExpectedException (typeof (ObjectDisposedException))]
363                 public void Disposed10 ()
364                 {
365                         Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
366                         s.Close();
367
368                         s.Receive (buf, 10, 0);
369                 }
370
371                 [Test]
372                 [ExpectedException (typeof (ObjectDisposedException))]
373                 public void Disposed11 ()
374                 {
375                         Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
376                         EndPoint ep = new IPEndPoint (IPAddress.Any, 31337);
377                         s.Close();
378
379                         s.Receive (buf, 0, 10, 0);
380                 }
381
382                 [Test]
383                 [ExpectedException (typeof (ObjectDisposedException))]
384                 public void Disposed12 ()
385                 {
386                         Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
387                         EndPoint ep = new IPEndPoint (IPAddress.Any, 31337);
388                         s.Close();
389
390                         s.ReceiveFrom (buf, 0, ref ep);
391                 }
392
393                 [Test]
394                 [ExpectedException (typeof (ObjectDisposedException))]
395                 public void Disposed13 ()
396                 {
397                         Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
398                         EndPoint ep = new IPEndPoint (IPAddress.Any, 31337);
399                         s.Close();
400
401                         s.ReceiveFrom (buf, 10, 0, ref ep);
402                 }
403
404                 [Test]
405                 [ExpectedException (typeof (ObjectDisposedException))]
406                 public void Disposed14 ()
407                 {
408                         Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
409                         EndPoint ep = new IPEndPoint (IPAddress.Any, 31337);
410                         s.Close();
411
412                         s.ReceiveFrom (buf, 0, 10, 0, ref ep);
413                 }
414
415                 [Test]
416                 [ExpectedException (typeof (ObjectDisposedException))]
417                 public void Disposed15 ()
418                 {
419                         Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
420                         s.Close();
421
422                         s.Send (buf);
423                 }
424
425                 [Test]
426                 [ExpectedException (typeof (ObjectDisposedException))]
427                 public void Disposed16 ()
428                 {
429                         Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
430                         s.Close();
431
432                         s.Send (buf, 0);
433                 }
434
435                 [Test]
436                 [ExpectedException (typeof (ObjectDisposedException))]
437                 public void Disposed17 ()
438                 {
439                         Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
440                         s.Close();
441
442                         s.Send (buf, 10, 0);
443                 }
444
445                 [Test]
446                 [ExpectedException (typeof (ObjectDisposedException))]
447                 public void Disposed18 ()
448                 {
449                         Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
450                         EndPoint ep = new IPEndPoint (IPAddress.Any, 31337);
451                         s.Close();
452
453                         s.Send (buf, 0, 10, 0);
454                 }
455
456                 [Test]
457                 [ExpectedException (typeof (ObjectDisposedException))]
458                 public void Disposed19 ()
459                 {
460                         Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
461                         EndPoint ep = new IPEndPoint (IPAddress.Any, 31337);
462                         s.Close();
463
464                         s.SendTo (buf, 0, ep);
465                 }
466
467                 [Test]
468                 [ExpectedException (typeof (ObjectDisposedException))]
469                 public void Disposed20 ()
470                 {
471                         Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
472                         EndPoint ep = new IPEndPoint (IPAddress.Any, 31337);
473                         s.Close();
474
475                         s.SendTo (buf, 10, 0, ep);
476                 }
477
478                 [Test]
479                 [ExpectedException (typeof (ObjectDisposedException))]
480                 public void Disposed21 ()
481                 {
482                         Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
483                         EndPoint ep = new IPEndPoint (IPAddress.Any, 31337);
484                         s.Close();
485
486                         s.SendTo (buf, 0, 10, 0, ep);
487                 }
488
489                 [Test]
490                 [ExpectedException (typeof (ObjectDisposedException))]
491                 public void Disposed22 ()
492                 {
493                         Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
494                         EndPoint ep = new IPEndPoint (IPAddress.Any, 31337);
495                         s.Close();
496
497                         s.SendTo (buf, ep);
498                 }
499
500                 [Test]
501                 [ExpectedException (typeof (ObjectDisposedException))]
502                 public void Disposed23 ()
503                 {
504                         Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
505                         EndPoint ep = new IPEndPoint (IPAddress.Any, 31337);
506                         s.Close();
507
508                         s.Shutdown (0);
509                 }
510
511                 [Test]
512                 public void GetHashCodeTest ()
513                 {
514                         Socket server = new Socket (AddressFamily.InterNetwork,
515                                 SocketType.Stream, ProtocolType.Tcp);
516                         IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
517                                                         9010);
518                         server.Bind (ep);
519                         server.Listen (1);
520
521                         Socket client = new Socket (AddressFamily.InterNetwork, 
522                                 SocketType.Stream, ProtocolType.Tcp);
523                         int hashcodeA = client.GetHashCode ();
524                         client.Connect (ep);
525                         int hashcodeB = client.GetHashCode ();
526                         Assert.AreEqual (hashcodeA, hashcodeB, "#1");
527                         client.Close ();
528                         int hashcodeC = client.GetHashCode ();
529 #if NET_2_0
530                         Assert.AreEqual (hashcodeB, hashcodeC, "#2");
531 #else
532                         Assert.IsFalse (hashcodeB == hashcodeC, "#2");
533 #endif
534                         server.Close ();
535                 }
536
537                 static ManualResetEvent SocketError_event = new ManualResetEvent (false);
538
539                 private static void SocketError_callback (IAsyncResult ar)
540                 {
541                         Socket sock = (Socket)ar.AsyncState;
542                         
543                         if(sock.Connected) {
544                                 sock.EndConnect (ar);
545                         }
546
547                         SocketError_event.Set ();
548                 }
549
550                 [Test]
551                 public void SocketError ()
552                 {
553                         Socket sock = new Socket (AddressFamily.InterNetwork,
554                                                   SocketType.Stream,
555                                                   ProtocolType.Tcp);
556                         IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
557                                                         BogusPort);
558                         
559                         SocketError_event.Reset ();
560
561                         sock.Blocking = false;
562                         sock.BeginConnect (ep, new AsyncCallback(SocketError_callback),
563                                 sock);
564
565                         if (SocketError_event.WaitOne (2000, false) == false) {
566                                 Assert.Fail ("SocketError wait timed out");
567                         }
568
569                         Assertion.AssertEquals ("SocketError #1", false,
570                                                 sock.Connected);
571
572                         int error;
573
574                         error = (int)sock.GetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Error);
575                         Assertion.AssertEquals ("SocketError #2", 10061,
576                                                 error);
577
578                         error = (int)sock.GetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Error);
579                         Assertion.AssertEquals ("SocketError #3", 10061,
580                                                 error);
581
582                         sock.Close ();
583                 }
584                 
585
586 #if NET_2_0
587                 [Test]
588                 public void SocketInformationCtor ()
589                 {
590                 }
591                 
592                 [Test]
593                 public void DontFragmentDefaultTcp ()
594                 {
595                         Socket sock = new Socket (AddressFamily.InterNetwork,
596                                                   SocketType.Stream,
597                                                   ProtocolType.Tcp);
598                         
599                         Assertion.AssertEquals ("DontFragmentDefaultTcp",
600                                                 false, sock.DontFragment);
601
602                         sock.Close ();
603                 }
604
605                 [Test]
606                 [Category ("NotOnMac")] // DontFragment doesn't work on Mac
607                 public void DontFragmentChangeTcp ()
608                 {
609                         Socket sock = new Socket (AddressFamily.InterNetwork,
610                                                   SocketType.Stream,
611                                                   ProtocolType.Tcp);
612                         
613                         sock.DontFragment = true;
614                         
615                         Assertion.AssertEquals ("DontFragmentChangeTcp",
616                                                 true, sock.DontFragment);
617
618                         sock.Close ();
619                 }
620                 
621                 [Test]
622                 public void DontFragmentDefaultUdp ()
623                 {
624                         Socket sock = new Socket (AddressFamily.InterNetwork,
625                                                   SocketType.Dgram,
626                                                   ProtocolType.Udp);
627                         
628                         Assertion.AssertEquals ("DontFragmentDefaultUdp",
629                                                 false, sock.DontFragment);
630
631                         sock.Close ();
632                 }
633
634                 [Test]
635                 [Category ("NotOnMac")] // DontFragment doesn't work on Mac
636                 public void DontFragmentChangeUdp ()
637                 {
638                         Socket sock = new Socket (AddressFamily.InterNetwork,
639                                                   SocketType.Dgram,
640                                                   ProtocolType.Udp);
641                         
642                         sock.DontFragment = true;
643                         
644                         Assertion.AssertEquals ("DontFragmentChangeUdp",
645                                                 true, sock.DontFragment);
646
647                         sock.Close ();
648                 }
649
650                 [Test]
651                 [ExpectedException (typeof(ObjectDisposedException))]
652                 public void DontFragmentClosed ()
653                 {
654                         Socket sock = new Socket (AddressFamily.InterNetwork,
655                                                   SocketType.Stream,
656                                                   ProtocolType.Tcp);
657                         
658                         sock.Close ();
659                         
660                         bool val = sock.DontFragment;
661                 }
662                 
663                 [Test]
664                 [Category ("NotWorking")] // Need to pick a non-IP AddressFamily that "works" on both mono and ms, this one only works on ms
665                 public void DontFragment ()
666                 {
667                         Socket sock = new Socket (AddressFamily.NetBios,
668                                                   SocketType.Seqpacket,
669                                                   ProtocolType.Unspecified);
670                         
671                         try {
672                                 sock.DontFragment = true;
673                                 Assert.Fail ("DontFragment #1");
674                         } catch (NotSupportedException) {
675                         } catch {
676                                 Assert.Fail ("DontFragment #2");
677                         } finally {
678                                 sock.Close ();
679                         }
680                 }
681                 
682                 [Test]
683                 public void EnableBroadcastDefaultTcp ()
684                 {
685                         Socket sock = new Socket (AddressFamily.InterNetwork,
686                                                   SocketType.Stream,
687                                                   ProtocolType.Tcp);
688                         
689                         try {
690                                 bool value = sock.EnableBroadcast;
691                                 Assert.Fail ("EnableBroadcastDefaultTcp #1");
692                         } catch (SocketException ex) {
693                                 Assert.AreEqual (10042, ex.ErrorCode, "EnableBroadcastDefaultTcp #2");
694                         } catch {
695                                 Assert.Fail ("EnableBroadcastDefaultTcp #2");
696                         } finally {
697                                 sock.Close ();
698                         }
699                 }
700
701                 [Test]
702                 public void EnableBroadcastDefaultUdp ()
703                 {
704                         Socket sock = new Socket (AddressFamily.InterNetwork,
705                                                   SocketType.Dgram,
706                                                   ProtocolType.Udp);
707                         
708                         Assertion.AssertEquals ("EnableBroadcastDefaultUdp",
709                                                 false, sock.EnableBroadcast);
710
711                         sock.Close ();
712                 }
713                 
714                 [Test]
715                 public void EnableBroadcastChangeTcp ()
716                 {
717                         Socket sock = new Socket (AddressFamily.InterNetwork,
718                                                   SocketType.Stream,
719                                                   ProtocolType.Tcp);
720                         
721                         try {
722                                 sock.EnableBroadcast = true;
723                                 Assert.Fail ("EnableBroadcastChangeTcp #1");
724                         } catch (SocketException ex) {
725                                 Assert.AreEqual (10042, ex.ErrorCode, "EnableBroadcastChangeTcp #2");
726                         } catch {
727                                 Assert.Fail ("EnableBroadcastChangeTcp #2");
728                         } finally {
729                                 sock.Close ();
730                         }
731                 }
732                 
733                 [Test]
734                 public void EnableBroadcastChangeUdp ()
735                 {
736                         Socket sock = new Socket (AddressFamily.InterNetwork,
737                                                   SocketType.Dgram,
738                                                   ProtocolType.Udp);
739                         
740                         sock.EnableBroadcast = true;
741                         
742                         Assertion.AssertEquals ("EnableBroadcastChangeUdp",
743                                                 true, sock.EnableBroadcast);
744
745                         sock.Close ();
746                 }
747
748                 [Test]
749                 [ExpectedException (typeof(ObjectDisposedException))]
750                 public void EnableBroadcastClosed ()
751                 {
752                         Socket sock = new Socket (AddressFamily.InterNetwork,
753                                                   SocketType.Dgram,
754                                                   ProtocolType.Udp);
755                         
756                         sock.Close ();
757                         
758                         bool val = sock.EnableBroadcast;
759                 }
760
761                 /* Can't test the default for ExclusiveAddressUse as
762                  * it's different on different versions and service
763                  * packs of windows
764                  */
765                 [Test]
766                 [Category ("NotWorking")] // Not supported on Linux
767                 public void ExclusiveAddressUseUnbound ()
768                 {
769                         Socket sock = new Socket (AddressFamily.InterNetwork,
770                                                   SocketType.Stream,
771                                                   ProtocolType.Tcp);
772                         
773                         sock.ExclusiveAddressUse = true;
774                         
775                         Assertion.AssertEquals ("ExclusiveAddressUseUnbound",
776                                                 true,
777                                                 sock.ExclusiveAddressUse);
778                         
779                         sock.Close ();
780                 }
781
782                 [Test]
783                 [ExpectedException (typeof(InvalidOperationException))]
784                 [Category ("NotWorking")] // Not supported on Linux
785                 public void ExclusiveAddressUseBound ()
786                 {
787                         Socket sock = new Socket (AddressFamily.InterNetwork,
788                                                   SocketType.Stream,
789                                                   ProtocolType.Tcp);
790                         
791                         sock.Bind (new IPEndPoint (IPAddress.Any, 1235));
792                         sock.ExclusiveAddressUse = true;
793                         sock.Close ();
794                 }
795
796                 [Test]
797                 [ExpectedException (typeof(ObjectDisposedException))]
798                 public void ExclusiveAddressUseClosed ()
799                 {
800                         Socket sock = new Socket (AddressFamily.InterNetwork,
801                                                   SocketType.Stream,
802                                                   ProtocolType.Tcp);
803                         
804                         sock.Close ();
805                         
806                         bool val = sock.ExclusiveAddressUse;
807                 }
808                 
809                 [Test]
810                 public void IsBoundTcp ()
811                 {
812                         Socket sock = new Socket (AddressFamily.InterNetwork,
813                                                   SocketType.Stream,
814                                                   ProtocolType.Tcp);
815                         IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
816                                                         BogusPort);
817                         
818                         Assertion.AssertEquals ("IsBoundTcp #1", false,
819                                                 sock.IsBound);
820                         
821                         sock.Bind (ep);
822                         Assertion.AssertEquals ("IsBoundTcp #2", true,
823                                                 sock.IsBound);
824
825                         sock.Listen (1);
826                         
827                         Socket sock2 = new Socket (AddressFamily.InterNetwork,
828                                                    SocketType.Stream,
829                                                    ProtocolType.Tcp);
830                         
831                         Assertion.AssertEquals ("IsBoundTcp #3", false,
832                                                 sock2.IsBound);
833                         
834                         sock2.Connect (ep);
835                         Assertion.AssertEquals ("IsBoundTcp #4", true,
836                                                 sock2.IsBound);
837                         
838                         sock2.Close ();
839                         Assertion.AssertEquals ("IsBoundTcp #5", true,
840                                                 sock2.IsBound);
841
842                         sock.Close ();
843                         Assertion.AssertEquals ("IsBoundTcp #6", true,
844                                                 sock.IsBound);
845                 }
846
847                 [Test]
848                 public void IsBoundUdp ()
849                 {
850                         Socket sock = new Socket (AddressFamily.InterNetwork,
851                                                   SocketType.Dgram,
852                                                   ProtocolType.Udp);
853                         IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
854                                                         BogusPort);
855                         
856                         Assertion.AssertEquals ("IsBoundUdp #1", false,
857                                                 sock.IsBound);
858                         
859                         sock.Bind (ep);
860                         Assertion.AssertEquals ("IsBoundUdp #2", true,
861                                                 sock.IsBound);
862                         
863                         sock.Close ();
864                         Assertion.AssertEquals ("IsBoundUdp #3", true,
865                                                 sock.IsBound);
866                         
867
868                         sock = new Socket (AddressFamily.InterNetwork,
869                                            SocketType.Dgram,
870                                            ProtocolType.Udp);
871                         
872                         Assertion.AssertEquals ("IsBoundUdp #4", false,
873                                                 sock.IsBound);
874                         
875                         sock.Connect (ep);
876                         Assertion.AssertEquals ("IsBoundUdp #5", true,
877                                                 sock.IsBound);
878                         
879                         sock.Close ();
880                         Assertion.AssertEquals ("IsBoundUdp #6", true,
881                                                 sock.IsBound);
882                 }
883
884                 [Test]
885                 /* Should not throw an exception */
886                 public void IsBoundClosed ()
887                 {
888                         Socket sock = new Socket (AddressFamily.InterNetwork,
889                                                   SocketType.Stream,
890                                                   ProtocolType.Tcp);
891                         
892                         sock.Close ();
893                         
894                         bool val = sock.IsBound;
895                 }
896                 
897                 /* Nothing much to test for LingerState */
898                 
899                 [Test]
900                 public void MulticastLoopbackDefaultTcp ()
901                 {
902                         Socket sock = new Socket (AddressFamily.InterNetwork,
903                                                   SocketType.Stream,
904                                                   ProtocolType.Tcp);
905                         
906                         try {
907                                 bool value = sock.MulticastLoopback;
908                                 Assert.Fail ("MulticastLoopbackDefaultTcp #1");
909                         } catch (SocketException ex) {
910                                 Assert.AreEqual (10042, ex.ErrorCode, "MulticastLoopbackDefaultTcp #2");
911                         } catch {
912                                 Assert.Fail ("MulticastLoopbackDefaultTcp #2");
913                         } finally {
914                                 sock.Close ();
915                         }
916                 }
917
918                 [Test]
919                 public void MulticastLoopbackChangeTcp ()
920                 {
921                         Socket sock = new Socket (AddressFamily.InterNetwork,
922                                                   SocketType.Stream,
923                                                   ProtocolType.Tcp);
924                         
925                         try {
926                                 sock.MulticastLoopback = false;
927                                 Assert.Fail ("MulticastLoopbackChangeTcp #1");
928                         } catch (SocketException ex) {
929                                 Assert.AreEqual (10042, ex.ErrorCode, "MulticastLoopbackChangeTcp #2");
930                         } catch {
931                                 Assert.Fail ("MulticastLoopbackChangeTcp #2");
932                         } finally {
933                                 sock.Close ();
934                         }
935                 }
936                 
937                 [Test]
938                 public void MulticastLoopbackDefaultUdp ()
939                 {
940                         Socket sock = new Socket (AddressFamily.InterNetwork,
941                                                   SocketType.Dgram,
942                                                   ProtocolType.Udp);
943                         
944                         Assertion.AssertEquals ("MulticastLoopbackDefaultUdp",
945                                                 true, sock.MulticastLoopback);
946                         
947                         sock.Close ();
948                 }
949                 
950                 [Test]
951                 public void MulticastLoopbackChangeUdp ()
952                 {
953                         Socket sock = new Socket (AddressFamily.InterNetwork,
954                                                   SocketType.Dgram,
955                                                   ProtocolType.Udp);
956                         
957                         sock.MulticastLoopback = false;
958                         
959                         Assertion.AssertEquals ("MulticastLoopbackChangeUdp",
960                                                 false, sock.MulticastLoopback);
961                         
962                         sock.Close ();
963                 }
964
965                 [Test]
966                 [ExpectedException (typeof(ObjectDisposedException))]
967                 public void MulticastLoopbackClosed ()
968                 {
969                         Socket sock = new Socket (AddressFamily.InterNetwork,
970                                                   SocketType.Stream,
971                                                   ProtocolType.Tcp);
972                         
973                         sock.Close ();
974                         
975                         bool val = sock.MulticastLoopback;
976                 }
977                 
978                 /* OSSupportsIPv6 depends on the environment */
979                 
980                 [Test]
981                 [Category("NotWorking")] // We have different defaults for perf reasons
982                 public void ReceiveBufferSizeDefault ()
983                 {
984                         Socket sock = new Socket (AddressFamily.InterNetwork,
985                                                   SocketType.Stream,
986                                                   ProtocolType.Tcp);
987                         
988                         Assertion.AssertEquals ("ReceiveBufferSizeDefault",
989                                                 8192, sock.ReceiveBufferSize);
990                         
991                         sock.Close ();
992                 }
993                 
994                 [Test]
995                 [Category("NotWorking")] // We have different defaults for perf reasons
996                 public void ReceiveBufferSizeDefaultUdp ()
997                 {
998                         Socket sock = new Socket (AddressFamily.InterNetwork,
999                                                   SocketType.Dgram,
1000                                                   ProtocolType.Udp);
1001                         
1002                         Assertion.AssertEquals ("ReceiveBufferSizeDefaultUdp",
1003                                                 8192, sock.ReceiveBufferSize);
1004                         
1005                         sock.Close ();
1006                 }
1007
1008                 [Test]
1009                 public void ReceiveBufferSizeChange ()
1010                 {
1011                         Socket sock = new Socket (AddressFamily.InterNetwork,
1012                                                   SocketType.Stream,
1013                                                   ProtocolType.Tcp);
1014                         
1015                         sock.ReceiveBufferSize = 16384;
1016                         
1017                         Assertion.AssertEquals ("ReceiveBufferSizeChange",
1018                                                 16384, sock.ReceiveBufferSize);
1019                         
1020                         sock.Close ();
1021                 }
1022
1023                 [Test]
1024                 [ExpectedException (typeof(ObjectDisposedException))]
1025                 public void ReceiveBufferSizeClosed ()
1026                 {
1027                         Socket sock = new Socket (AddressFamily.InterNetwork,
1028                                                   SocketType.Stream,
1029                                                   ProtocolType.Tcp);
1030                         
1031                         sock.Close ();
1032                         
1033                         int val = sock.ReceiveBufferSize;
1034                 }
1035                 
1036                 [Test]
1037                 [Category("NotWorking")] // We have different defaults for perf reasons
1038                 public void SendBufferSizeDefault ()
1039                 {
1040                         Socket sock = new Socket (AddressFamily.InterNetwork,
1041                                                   SocketType.Stream,
1042                                                   ProtocolType.Tcp);
1043                         
1044                         Assertion.AssertEquals ("SendBufferSizeDefault",
1045                                                 8192, sock.SendBufferSize);
1046                         
1047                         sock.Close ();
1048                 }
1049                 
1050                 [Test]
1051                 [Category("NotWorking")] // We have different defaults for perf reasons
1052                 public void SendBufferSizeDefaultUdp ()
1053                 {
1054                         Socket sock = new Socket (AddressFamily.InterNetwork,
1055                                                   SocketType.Dgram,
1056                                                   ProtocolType.Udp);
1057                         
1058                         Assertion.AssertEquals ("SendBufferSizeDefaultUdp",
1059                                                 8192, sock.SendBufferSize);
1060                         
1061                         sock.Close ();
1062                 }
1063
1064                 [Test]
1065                 public void SendBufferSizeChange ()
1066                 {
1067                         Socket sock = new Socket (AddressFamily.InterNetwork,
1068                                                   SocketType.Stream,
1069                                                   ProtocolType.Tcp);
1070                         
1071                         sock.SendBufferSize = 16384;
1072                         
1073                         Assertion.AssertEquals ("SendBufferSizeChange",
1074                                                 16384, sock.SendBufferSize);
1075                         
1076                         sock.Close ();
1077                 }
1078
1079                 [Test]
1080                 [ExpectedException (typeof(ObjectDisposedException))]
1081                 public void SendBufferSizeClosed ()
1082                 {
1083                         Socket sock = new Socket (AddressFamily.InterNetwork,
1084                                                   SocketType.Stream,
1085                                                   ProtocolType.Tcp);
1086                         
1087                         sock.Close ();
1088                         
1089                         int val = sock.SendBufferSize;
1090                 }
1091                 
1092                 /* No test for TTL default as it's platform dependent */
1093                 [Test]
1094                 public void TtlChange ()
1095                 {
1096                         Socket sock = new Socket (AddressFamily.InterNetwork,
1097                                                   SocketType.Stream,
1098                                                   ProtocolType.Tcp);
1099                         
1100                         sock.Ttl = 255;
1101                         
1102                         Assertion.AssertEquals ("TtlChange", 255, sock.Ttl);
1103                         
1104                         sock.Close ();
1105                 }
1106
1107                 [Test]
1108                 [Category ("NotOnMac")] // Mac doesn't throw when overflowing the ttl
1109                 public void TtlChangeOverflow ()
1110                 {
1111                         Socket sock = new Socket (AddressFamily.InterNetwork,
1112                                                   SocketType.Stream,
1113                                                   ProtocolType.Tcp);
1114                         
1115                         try {
1116                                 sock.Ttl = 256;
1117                                 Assert.Fail ("TtlChangeOverflow #1");
1118                         } catch (SocketException ex) {
1119                                 Assert.AreEqual (10022, ex.ErrorCode,
1120                                                  "TtlChangeOverflow #2");
1121                         } catch {
1122                                 Assert.Fail ("TtlChangeoverflow #3");
1123                         } finally {
1124                                 sock.Close ();
1125                         }
1126                 }
1127                 
1128 /* Apparently you can set TTL=0 on the ms runtime!!
1129                         try {
1130                                 sock.Ttl = 0;
1131                                 Assert.Fail ("TtlChangeOverflow #4");
1132                         } catch (SocketException ex) {
1133                                 Assert.AreEqual (10022, ex.ErrorCode,
1134                                                  "TtlChangeOverflow #5");
1135                         } catch {
1136                                 Assert.Fail ("TtlChangeOverflow #6");
1137                         } finally {
1138                                 sock.Close ();
1139                         }
1140 */
1141
1142                 [Test]
1143                 [ExpectedException (typeof(ObjectDisposedException))]
1144                 public void TtlClosed ()
1145                 {
1146                         Socket sock = new Socket (AddressFamily.InterNetwork,
1147                                                   SocketType.Stream,
1148                                                   ProtocolType.Tcp);
1149                         
1150                         sock.Close ();
1151                         
1152                         int val = sock.Ttl;
1153                 }
1154                 
1155                 [Test]
1156                 public void UseOnlyOverlappedIODefault ()
1157                 {
1158                         Socket sock = new Socket (AddressFamily.InterNetwork,
1159                                                   SocketType.Stream,
1160                                                   ProtocolType.Tcp);
1161                         
1162                         Assertion.AssertEquals ("UseOnlyOverlappedIODefault",
1163                                                 false,
1164                                                 sock.UseOnlyOverlappedIO);
1165                         
1166                         sock.Close ();
1167                 }
1168
1169                 //
1170                 // We need this because the Linux kernel in certain configurations
1171                 // will end up rounding up the values passed on to the kernel
1172                 // for socket send/receive timeouts.
1173                 //
1174                 int Approximate (int target, int value)
1175                 {
1176                         int epsilon = 10;
1177                         
1178                         if (value > target-10 && value < target+10)
1179                                 return target;
1180                         return value;
1181                 }
1182                 
1183                 [Test]
1184                 public void UseOnlyOverlappedIOChange ()
1185                 {
1186                         Socket sock = new Socket (AddressFamily.InterNetwork,
1187                                                   SocketType.Stream,
1188                                                   ProtocolType.Tcp);
1189                         
1190                         sock.UseOnlyOverlappedIO = true;
1191                         
1192                         Assertion.AssertEquals ("UseOnlyOverlappedIOChange",
1193                                                 true,
1194                                                 sock.UseOnlyOverlappedIO);
1195                         
1196                         sock.Close ();
1197                 }
1198
1199                 [Test]
1200                 /* Should not throw an exception */
1201                 public void UseOnlyOverlappedIOClosed ()
1202                 {
1203                         Socket sock = new Socket (AddressFamily.InterNetwork,
1204                                                   SocketType.Stream,
1205                                                   ProtocolType.Tcp);
1206                         
1207                         sock.Close ();
1208                         
1209                         bool val = sock.UseOnlyOverlappedIO;
1210                 }
1211                 
1212                 [Test]
1213                 public void SendTimeoutDefault ()
1214                 {
1215                         Socket sock = new Socket (AddressFamily.InterNetwork,
1216                                                   SocketType.Stream,
1217                                                   ProtocolType.Tcp);
1218                         
1219                         Assertion.AssertEquals ("SendTimeoutDefault",
1220                                                 0, sock.SendTimeout);
1221                         
1222                         sock.Close ();
1223                 }
1224
1225                 [Test]
1226                 public void SendTimeoutChange ()
1227                 {
1228                         Socket sock = new Socket (AddressFamily.InterNetwork,
1229                                                   SocketType.Stream,
1230                                                   ProtocolType.Tcp);
1231                         
1232                         /* Should be rounded up to 500, according to
1233                          * the MSDN docs, but the MS runtime doesn't
1234                          */
1235                         sock.SendTimeout = 50;
1236                         Assertion.AssertEquals ("SendTimeoutChange #1",
1237                                                 50, Approximate (50, sock.SendTimeout));
1238                         
1239                         sock.SendTimeout = 2000;
1240                         Assertion.AssertEquals ("SendTimeoutChange #2",
1241                                                 2000, Approximate (2000, sock.SendTimeout));
1242                         
1243                         sock.SendTimeout = 0;
1244                         Assertion.AssertEquals ("SendTimeoutChange #3",
1245                                                 0, Approximate (0, sock.SendTimeout));
1246                         
1247                         /* Should be the same as setting 0 */
1248                         sock.SendTimeout = -1;
1249                         Assertion.AssertEquals ("SendTimeoutChange #4",
1250                                                 0, sock.SendTimeout);
1251
1252                         sock.SendTimeout = 65536;
1253                         Assertion.AssertEquals ("SendTimeoutChange #5",
1254                                                 65536, Approximate (65536, sock.SendTimeout));
1255                         
1256                         try {
1257                                 sock.SendTimeout = -2;
1258                                 Assert.Fail ("SendTimeoutChange #8");
1259                         } catch (ArgumentOutOfRangeException) {
1260                         } catch {
1261                                 Assert.Fail ("SendTimeoutChange #9");
1262                         } finally {
1263                                 sock.Close ();
1264                         }
1265                 }
1266
1267                 [Test]
1268                 [ExpectedException (typeof(ObjectDisposedException))]
1269                 public void SendTimeoutClosed ()
1270                 {
1271                         Socket sock = new Socket (AddressFamily.InterNetwork,
1272                                                   SocketType.Stream,
1273                                                   ProtocolType.Tcp);
1274                         
1275                         sock.Close ();
1276                         
1277                         int val = sock.SendTimeout;
1278                 }
1279                 
1280                 [Test]
1281                 public void ReceiveTimeoutDefault ()
1282                 {
1283                         Socket sock = new Socket (AddressFamily.InterNetwork,
1284                                                   SocketType.Stream,
1285                                                   ProtocolType.Tcp);
1286                         
1287                         Assertion.AssertEquals ("ReceiveTimeoutDefault",
1288                                                 0, sock.ReceiveTimeout);
1289                         
1290                         sock.Close ();
1291                 }
1292
1293                 [Test]
1294                 public void ReceiveTimeoutChange ()
1295                 {
1296                         Socket sock = new Socket (AddressFamily.InterNetwork,
1297                                                   SocketType.Stream,
1298                                                   ProtocolType.Tcp);
1299                         
1300                         sock.ReceiveTimeout = 50;
1301                         Assertion.AssertEquals ("ReceiveTimeoutChange #1",
1302                                                 50, Approximate (50, sock.ReceiveTimeout));
1303                         
1304                         sock.ReceiveTimeout = 2000;
1305                         Assertion.AssertEquals ("ReceiveTimeoutChange #2",
1306                                                 2000, Approximate (2000, sock.ReceiveTimeout));
1307                         
1308                         sock.ReceiveTimeout = 0;
1309                         Assertion.AssertEquals ("ReceiveTimeoutChange #3",
1310                                                 0, sock.ReceiveTimeout);
1311                         
1312                         /* Should be the same as setting 0 */
1313                         sock.ReceiveTimeout = -1;
1314                         Assertion.AssertEquals ("ReceiveTimeoutChange #4",
1315                                                 0, sock.ReceiveTimeout);
1316
1317                         sock.ReceiveTimeout = 65536;
1318                         Assertion.AssertEquals ("ReceiveTimeoutChange #5",
1319                                                 65536, Approximate (65536, sock.ReceiveTimeout));
1320                         
1321                         try {
1322                                 sock.ReceiveTimeout = -2;
1323                                 Assert.Fail ("ReceiveTimeoutChange #8");
1324                         } catch (ArgumentOutOfRangeException) {
1325                         } catch {
1326                                 Assert.Fail ("ReceiveTimeoutChange #9");
1327                         } finally {
1328                                 sock.Close ();
1329                         }
1330                 }
1331
1332                 [Test]
1333                 [ExpectedException (typeof(ObjectDisposedException))]
1334                 public void ReceiveTimeoutClosed ()
1335                 {
1336                         Socket sock = new Socket (AddressFamily.InterNetwork,
1337                                                   SocketType.Stream,
1338                                                   ProtocolType.Tcp);
1339                         
1340                         sock.Close ();
1341                         
1342                         int val = sock.ReceiveTimeout;
1343                 }
1344                 
1345                 [Test]
1346                 public void NoDelayDefaultTcp ()
1347                 {
1348                         Socket sock = new Socket (AddressFamily.InterNetwork,
1349                                                   SocketType.Stream,
1350                                                   ProtocolType.Tcp);
1351                         
1352                         Assertion.AssertEquals ("NoDelayDefaultTcp", false,
1353                                                 sock.NoDelay);
1354                         
1355                         sock.Close ();
1356                 }
1357
1358                 [Test]
1359                 public void NoDelayChangeTcp ()
1360                 {
1361                         Socket sock = new Socket (AddressFamily.InterNetwork,
1362                                                   SocketType.Stream,
1363                                                   ProtocolType.Tcp);
1364                         
1365                         sock.NoDelay = true;
1366                         
1367                         Assertion.AssertEquals ("NoDelayChangeTcp", true,
1368                                                 sock.NoDelay);
1369                         
1370                         sock.Close ();
1371                 }
1372                 
1373                 [Test]
1374                 public void NoDelayDefaultUdp ()
1375                 {
1376                         Socket sock = new Socket (AddressFamily.InterNetwork,
1377                                                   SocketType.Dgram,
1378                                                   ProtocolType.Udp);
1379                         
1380                         try {
1381                                 bool val = sock.NoDelay;
1382                                 Assert.Fail ("NoDelayDefaultUdp #1");
1383                         } catch (SocketException ex) {
1384                                 Assert.AreEqual (10042, ex.ErrorCode,
1385                                                  "NoDelayDefaultUdp #2");
1386                         } catch {
1387                                 Assert.Fail ("NoDelayDefaultUdp #3");
1388                         } finally {
1389                                 sock.Close ();
1390                         }
1391                 }
1392
1393                 [Test]
1394                 public void NoDelayChangeUdp ()
1395                 {
1396                         Socket sock = new Socket (AddressFamily.InterNetwork,
1397                                                   SocketType.Dgram,
1398                                                   ProtocolType.Udp);
1399                         
1400                         try {
1401                                 sock.NoDelay = true;
1402                                 Assert.Fail ("NoDelayChangeUdp #1");
1403                         } catch (SocketException ex) {
1404                                 Assert.AreEqual (10042, ex.ErrorCode,
1405                                                  "NoDelayChangeUdp #2");
1406                         } catch {
1407                                 Assert.Fail ("NoDelayChangeUdp #3");
1408                         } finally {
1409                                 sock.Close ();
1410                         }
1411                 }
1412                 
1413                 [Test]
1414                 [ExpectedException (typeof(ObjectDisposedException))]
1415                 public void NoDelayClosed ()
1416                 {
1417                         Socket sock = new Socket (AddressFamily.InterNetwork,
1418                                                   SocketType.Stream,
1419                                                   ProtocolType.Tcp);
1420                         
1421                         sock.Close ();
1422                         
1423                         bool val = sock.NoDelay;
1424                 }
1425
1426                 static bool BAAccepted = false;
1427                 static Socket BASocket = null;
1428                 static ManualResetEvent BACalledBack = new ManualResetEvent (false);
1429                 
1430                 private static void BACallback (IAsyncResult asyncResult)
1431                 {
1432                         Socket sock = (Socket)asyncResult.AsyncState;
1433                         
1434                         BASocket = sock.EndAccept (asyncResult);
1435                         
1436                         BAAccepted = true;
1437                         BACalledBack.Set ();
1438                 }
1439                 
1440                 [Test]
1441                 [ExpectedException (typeof(InvalidOperationException))]
1442                 public void BeginAcceptNotBound ()
1443                 {
1444                         Socket sock = new Socket (AddressFamily.InterNetwork,
1445                                                   SocketType.Stream,
1446                                                   ProtocolType.Tcp);
1447
1448                         sock.BeginAccept (BACallback, sock);
1449                         
1450                         sock.Close ();
1451                 }
1452                 
1453                 [Test]
1454                 [ExpectedException (typeof(InvalidOperationException))]
1455                 public void BeginAcceptNotListening ()
1456                 {
1457                         Socket sock = new Socket (AddressFamily.InterNetwork,
1458                                                   SocketType.Stream,
1459                                                   ProtocolType.Tcp);
1460
1461                         sock.Bind (new IPEndPoint (IPAddress.Any, 1236));
1462                         
1463                         sock.BeginAccept (BACallback, sock);
1464                         
1465                         sock.Close ();
1466                 }
1467
1468                 [Test]
1469                 public void BeginAccept ()
1470                 {
1471                         Socket sock = new Socket (AddressFamily.InterNetwork,
1472                                                   SocketType.Stream,
1473                                                   ProtocolType.Tcp);
1474                         IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
1475                                                         1237);
1476                         
1477                         sock.Bind (ep);
1478                         sock.Listen (1);
1479                         
1480                         BACalledBack.Reset ();
1481                         
1482                         sock.BeginAccept (BACallback, sock);
1483
1484                         Socket conn = new Socket (AddressFamily.InterNetwork,
1485                                                   SocketType.Stream,
1486                                                   ProtocolType.Tcp);
1487                         
1488                         conn.Connect (ep);
1489
1490                         if (BACalledBack.WaitOne (2000, false) == false) {
1491                                 Assert.Fail ("BeginAccept wait timed out");
1492                         }
1493                         
1494                         Assertion.AssertEquals ("BeginAccept #1", true,
1495                                                 BAAccepted);
1496                         Assertion.AssertEquals ("BeginAccept #2", true,
1497                                                 BASocket.Connected);
1498                         Assertion.AssertEquals ("BeginAccept #3", false,
1499                                                 sock.Connected);
1500                         Assertion.AssertEquals ("BeginAccept #4", true,
1501                                                 conn.Connected);
1502
1503                         BASocket.Close ();
1504                         conn.Close ();
1505                         sock.Close ();
1506                 }
1507
1508                 [Test]
1509                 [ExpectedException (typeof(ObjectDisposedException))]
1510                 public void BeginAcceptClosed ()
1511                 {
1512                         Socket sock = new Socket (AddressFamily.InterNetwork,
1513                                                   SocketType.Stream,
1514                                                   ProtocolType.Tcp);
1515                         
1516                         sock.Close ();
1517                         
1518                         sock.BeginAccept (BACallback, sock);
1519                 }
1520
1521                 static bool BADAccepted = false;
1522                 static Socket BADSocket = null;
1523                 static byte[] BADBytes;
1524                 static int BADByteCount;
1525                 static ManualResetEvent BADCalledBack = new ManualResetEvent (false);
1526                 
1527                 private static void BADCallback (IAsyncResult asyncResult)
1528                 {
1529                         Socket sock = (Socket)asyncResult.AsyncState;
1530                         
1531                         BADSocket = sock.EndAccept (out BADBytes,
1532                                                     out BADByteCount,
1533                                                     asyncResult);
1534                         
1535                         BADAccepted = true;
1536                         BADCalledBack.Set ();
1537                 }
1538
1539                 [Test]
1540                 public void BeginAcceptData ()
1541                 {
1542                         Socket sock = new Socket (AddressFamily.InterNetwork,
1543                                                   SocketType.Stream,
1544                                                   ProtocolType.Tcp);
1545                         IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
1546                                                         1238);
1547                         
1548                         sock.Bind (ep);
1549                         sock.Listen (1);
1550                         
1551                         BADCalledBack.Reset ();
1552                         
1553                         sock.BeginAccept (256, BADCallback, sock);
1554
1555                         Socket conn = new Socket (AddressFamily.InterNetwork,
1556                                                   SocketType.Stream,
1557                                                   ProtocolType.Tcp);
1558                         byte[] send_bytes = new byte[] {10, 11, 12, 13};
1559                         
1560                         conn.Connect (ep);
1561                         conn.Send (send_bytes);
1562
1563                         if (BADCalledBack.WaitOne (2000, false) == false) {
1564                                 Assert.Fail ("BeginAcceptData wait timed out");
1565                         }
1566                         
1567                         Assertion.AssertEquals ("BeginAcceptData #1", true,
1568                                                 BADAccepted);
1569                         Assertion.AssertEquals ("BeginAcceptData #2", true,
1570                                                 BADSocket.Connected);
1571                         Assertion.AssertEquals ("BeginAcceptData #3", false,
1572                                                 sock.Connected);
1573                         Assertion.AssertEquals ("BeginAcceptData #4", true,
1574                                                 conn.Connected);
1575                         Assertion.AssertEquals ("BeginAcceptData #5",
1576                                                 send_bytes.Length,
1577                                                 BADByteCount);
1578                         
1579                         /* The MS runtime gives the returned data in a
1580                          * much bigger array.  TODO: investigate
1581                          * whether it the size correlates to the first
1582                          * parameter in BeginAccept()
1583                          */
1584                         Assert.IsFalse (BADBytes.Length == send_bytes.Length,
1585                                         "BeginAcceptData #6");
1586
1587                         for(int i = 0; i < send_bytes.Length; i++) {
1588                                 Assertion.AssertEquals ("BeginAcceptData #" + (i+7).ToString (), send_bytes[i], BADBytes[i]);
1589                         }
1590
1591                         BADSocket.Close ();
1592                         conn.Close ();
1593                         sock.Close ();
1594                 }
1595
1596                 [Test]
1597                 [ExpectedException (typeof(ObjectDisposedException))]
1598                 public void BeginAcceptDataClosed ()
1599                 {
1600                         Socket sock = new Socket (AddressFamily.InterNetwork,
1601                                                   SocketType.Stream,
1602                                                   ProtocolType.Tcp);
1603                         
1604                         sock.Close ();
1605                         
1606                         sock.BeginAccept (256, BADCallback, sock);
1607                 }
1608
1609                 [Test]
1610                 public void BeginAcceptSocketUdp ()
1611                 {
1612                         Socket sock = new Socket (AddressFamily.InterNetwork,
1613                                                   SocketType.Stream,
1614                                                   ProtocolType.Tcp);
1615                         Socket acc = new Socket (AddressFamily.InterNetwork,
1616                                                  SocketType.Dgram,
1617                                                  ProtocolType.Udp);
1618                         
1619                         IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
1620                                                         1239);
1621                         
1622                         sock.Bind (ep);
1623                         sock.Listen (1);
1624                         
1625                         try {
1626                                 sock.BeginAccept (acc, 256, BADCallback, sock);
1627                                 Assert.Fail ("BeginAcceptSocketUdp #1");
1628                         } catch (SocketException ex) {
1629                                 Assertion.AssertEquals ("BeginAcceptSocketUdp #2", 10022, ex.ErrorCode);
1630                         } catch {
1631                                 Assert.Fail ("BeginAcceptSocketUdp #3");
1632                         } finally {
1633                                 acc.Close ();
1634                                 sock.Close ();
1635                         }
1636                 }
1637                 
1638                 [Test]
1639                 public void BeginAcceptSocketBound ()
1640                 {
1641                         Socket sock = new Socket (AddressFamily.InterNetwork,
1642                                                   SocketType.Stream,
1643                                                   ProtocolType.Tcp);
1644                         Socket acc = new Socket (AddressFamily.InterNetwork,
1645                                                  SocketType.Stream,
1646                                                  ProtocolType.Tcp);
1647                         
1648                         IPEndPoint ep1 = new IPEndPoint (IPAddress.Loopback,
1649                                                          1240);
1650                         
1651                         IPEndPoint ep2 = new IPEndPoint (IPAddress.Loopback,
1652                                                          1241);
1653                         
1654                         sock.Bind (ep1);
1655                         sock.Listen (1);
1656
1657                         acc.Bind (ep2);
1658                         
1659                         try {
1660                                 sock.BeginAccept (acc, 256, BADCallback, sock);
1661                                 Assert.Fail ("BeginAcceptSocketBound #1");
1662                         } catch (InvalidOperationException) {
1663                         } catch {
1664                                 Assert.Fail ("BeginAcceptSocketBound #2");
1665                         } finally {
1666                                 acc.Close ();
1667                                 sock.Close ();
1668                         }
1669                 }
1670                 
1671                 [Test]
1672                 public void BeginAcceptSocket ()
1673                 {
1674                         Socket sock = new Socket (AddressFamily.InterNetwork,
1675                                                   SocketType.Stream,
1676                                                   ProtocolType.Tcp);
1677                         Socket acc = new Socket (AddressFamily.InterNetwork,
1678                                                  SocketType.Stream,
1679                                                  ProtocolType.Tcp);
1680                         
1681                         IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
1682                                                         1242);
1683                         
1684                         sock.Bind (ep);
1685                         sock.Listen (1);
1686                         
1687                         BADCalledBack.Reset ();
1688                         
1689                         sock.BeginAccept (acc, 256, BADCallback, sock);
1690
1691                         Socket conn = new Socket (AddressFamily.InterNetwork,
1692                                                   SocketType.Stream,
1693                                                   ProtocolType.Tcp);
1694                         byte[] send_bytes = new byte[] {10, 11, 12, 13};
1695                         
1696                         conn.Connect (ep);
1697                         conn.Send (send_bytes);
1698
1699                         if (BADCalledBack.WaitOne (2000, false) == false) {
1700                                 Assert.Fail ("BeginAcceptSocket wait timed out");
1701                         }
1702                         
1703                         Assertion.AssertEquals ("BeginAcceptSocket #1", true,
1704                                                 BADAccepted);
1705                         Assertion.AssertEquals ("BeginAcceptSocket #2", true,
1706                                                 BADSocket.Connected);
1707                         Assertion.AssertEquals ("BeginAcceptSocket #3", false,
1708                                                 sock.Connected);
1709                         Assertion.AssertEquals ("BeginAcceptSocket #4", true,
1710                                                 conn.Connected);
1711                         Assertion.AssertEquals ("BeginAcceptSocket #5",
1712                                                 send_bytes.Length,
1713                                                 BADByteCount);
1714                         Assertion.AssertEquals ("BeginAcceptSocket #6",
1715                                                 AddressFamily.InterNetwork,
1716                                                 acc.AddressFamily);
1717                         Assertion.AssertEquals ("BeginAcceptSocket #7",
1718                                                 SocketType.Stream,
1719                                                 acc.SocketType);
1720                         Assertion.AssertEquals ("BeginAcceptSocket #8",
1721                                                 ProtocolType.Tcp,
1722                                                 acc.ProtocolType);
1723                         Assertion.AssertEquals ("BeginAcceptSocket #9",
1724                                                 conn.LocalEndPoint,
1725                                                 acc.RemoteEndPoint);
1726                         
1727                         /* The MS runtime gives the returned data in a
1728                          * much bigger array.  TODO: investigate
1729                          * whether it the size correlates to the first
1730                          * parameter in BeginAccept()
1731                          */
1732                         Assert.IsFalse (BADBytes.Length == send_bytes.Length,
1733                                         "BeginAcceptSocket #10");
1734
1735                         for(int i = 0; i < send_bytes.Length; i++) {
1736                                 Assertion.AssertEquals ("BeginAcceptSocket #" + (i+11).ToString (), send_bytes[i], BADBytes[i]);
1737                         }
1738
1739                         BADSocket.Close ();
1740                         conn.Close ();
1741                         acc.Close ();
1742                         sock.Close ();
1743                 }
1744
1745                 [Test]
1746                 public void BeginAcceptSocketClosed ()
1747                 {
1748                         Socket sock = new Socket (AddressFamily.InterNetwork,
1749                                                   SocketType.Stream,
1750                                                   ProtocolType.Tcp);
1751                         Socket acc = new Socket (AddressFamily.InterNetwork,
1752                                                  SocketType.Stream,
1753                                                  ProtocolType.Tcp);
1754                         
1755                         sock.Close ();
1756                         
1757                         try {
1758                                 sock.BeginAccept (acc, 256, BADCallback, null);
1759                                 Assert.Fail ("BeginAcceptSocketClosed #1");
1760                         } catch (ObjectDisposedException) {
1761                         } catch {
1762                                 Assert.Fail ("BeginAcceptSocketClosed #2");
1763                         } finally {
1764                                 acc.Close ();
1765                         }
1766                 }
1767
1768                 [Test]
1769                 public void BeginAcceptSocketAccClosed ()
1770                 {
1771                         Socket sock = new Socket (AddressFamily.InterNetwork,
1772                                                   SocketType.Stream,
1773                                                   ProtocolType.Tcp);
1774                         Socket acc = new Socket (AddressFamily.InterNetwork,
1775                                                  SocketType.Stream,
1776                                                  ProtocolType.Tcp);
1777                         IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
1778                                                         1243);
1779
1780                         sock.Bind (ep);
1781                         sock.Listen (1);
1782                         
1783                         acc.Close ();
1784                         
1785                         BADCalledBack.Reset ();
1786                         
1787                         try {
1788                                 sock.BeginAccept (acc, 256, BADCallback, null);
1789                                 Assert.Fail ("BeginAcceptSocketAccClosed #1");
1790                         } catch (ObjectDisposedException) {
1791                         } catch {
1792                                 Assert.Fail ("BeginAcceptSocketAccClosed #2");
1793                         } finally {
1794                                 sock.Close ();
1795                         }
1796                 }
1797                 
1798                 static bool BCConnected = false;
1799                 static ManualResetEvent BCCalledBack = new ManualResetEvent (false);
1800                 
1801                 private static void BCCallback (IAsyncResult asyncResult)
1802                 {
1803                         Socket sock = (Socket)asyncResult.AsyncState;
1804                         
1805                         sock.EndConnect (asyncResult);
1806                         BCConnected = true;
1807                         
1808                         BCCalledBack.Set ();
1809                 }
1810                 
1811                 [Test]
1812                 public void BeginConnectAddressPort ()
1813                 {
1814                         Socket sock = new Socket (AddressFamily.InterNetwork,
1815                                                   SocketType.Stream,
1816                                                   ProtocolType.Tcp);
1817                         Socket listen = new Socket (AddressFamily.InterNetwork,
1818                                                     SocketType.Stream,
1819                                                     ProtocolType.Tcp);
1820                         IPAddress ip = IPAddress.Loopback;
1821                         IPEndPoint ep = new IPEndPoint (ip, 1244);
1822
1823                         listen.Bind (ep);
1824                         listen.Listen (1);
1825                         
1826                         BCCalledBack.Reset ();
1827                         
1828                         BCConnected = false;
1829                         
1830                         sock.BeginConnect (ip, 1244, BCCallback, sock);
1831
1832                         if (BCCalledBack.WaitOne (2000, false) == false) {
1833                                 Assert.Fail ("BeginConnectAddressPort wait timed out");
1834                         }
1835                         
1836                         Assertion.AssertEquals ("BeginConnectAddressPort #1",
1837                                                 true, BCConnected);
1838                         
1839                         sock.Close ();
1840                         listen.Close ();
1841                 }
1842
1843                 [Test]
1844                 public void BeginConnectAddressPortNull ()
1845                 {
1846                         Socket sock = new Socket (AddressFamily.InterNetwork,
1847                                                   SocketType.Stream,
1848                                                   ProtocolType.Tcp);
1849                         IPAddress ip = null;
1850
1851                         try {
1852                                 sock.BeginConnect (ip, 1244, BCCallback,
1853                                                    sock);
1854                                 Assert.Fail ("BeginConnectAddressPortNull #1");
1855                         } catch (ArgumentNullException) {
1856                         } catch {
1857                                 Assert.Fail ("BeginConnectAddressPortNull #2");
1858                         } finally {
1859                                 sock.Close ();
1860                         }
1861                 }
1862
1863                 [Test]
1864                 public void BeginConnectAddressPortListen ()
1865                 {
1866                         Socket sock = new Socket (AddressFamily.InterNetwork,
1867                                                   SocketType.Stream,
1868                                                   ProtocolType.Tcp);
1869                         IPAddress ip = IPAddress.Loopback;
1870                         IPEndPoint ep = new IPEndPoint (ip, 1245);
1871
1872                         sock.Bind (ep);
1873                         sock.Listen (1);
1874                         
1875                         try {
1876                                 sock.BeginConnect (ip, 1245, BCCallback, sock);
1877                                 Assert.Fail ("BeginConnectAddressPortListen #1");
1878                         } catch (InvalidOperationException) {
1879                         } catch {
1880                                 Assert.Fail ("BeginConnectAddressPortListen #2");
1881                         } finally {
1882                                 sock.Close ();
1883                         }
1884                 }
1885                 
1886                 [Test]
1887                 [ExpectedException (typeof(ObjectDisposedException))]
1888                 public void BeginConnectAddressPortClosed ()
1889                 {
1890                         Socket sock = new Socket (AddressFamily.InterNetwork,
1891                                                   SocketType.Stream,
1892                                                   ProtocolType.Tcp);
1893                         IPAddress ip = IPAddress.Loopback;
1894                         
1895                         sock.Close ();
1896                         
1897                         sock.BeginConnect (ip, 1244, BCCallback, sock);
1898                 }
1899                 
1900                 [Test]
1901                 [Category ("NotOnMac")] // MacOSX will block attempting to connect to 127.0.0.4 causing the test to fail
1902                 public void BeginConnectMultiple ()
1903                 {
1904                         Socket sock = new Socket (AddressFamily.InterNetwork,
1905                                                   SocketType.Stream,
1906                                                   ProtocolType.Tcp);
1907                         Socket listen = new Socket (AddressFamily.InterNetwork,
1908                                                     SocketType.Stream,
1909                                                     ProtocolType.Tcp);
1910                         IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
1911                                                         1246);
1912                         IPAddress[] ips = new IPAddress[4];
1913                         
1914                         ips[0] = IPAddress.Parse ("127.0.0.4");
1915                         ips[1] = IPAddress.Parse ("127.0.0.3");
1916                         ips[2] = IPAddress.Parse ("127.0.0.2");
1917                         ips[3] = IPAddress.Parse ("127.0.0.1");
1918
1919                         listen.Bind (ep);
1920                         listen.Listen (1);
1921                         
1922                         BCCalledBack.Reset ();
1923                         
1924                         BCConnected = false;
1925                         
1926                         sock.BeginConnect (ips, 1246, BCCallback, sock);
1927                         
1928                         /* Longer wait here, because the ms runtime
1929                          * takes a lot longer to not connect
1930                          */
1931                         if (BCCalledBack.WaitOne (10000, false) == false) {
1932                                 Assert.Fail ("BeginConnectMultiple wait failed");
1933                         }
1934                         
1935                         Assertion.AssertEquals ("BeginConnectMultiple #1",
1936                                                 true, BCConnected);
1937                         Assertion.AssertEquals ("BeginConnectMultiple #2",
1938                                                 AddressFamily.InterNetwork,
1939                                                 sock.RemoteEndPoint.AddressFamily);
1940                         IPEndPoint remep = (IPEndPoint)sock.RemoteEndPoint;
1941                         
1942                         Assertion.AssertEquals ("BeginConnectMultiple #2",
1943                                                 IPAddress.Loopback,
1944                                                 remep.Address);
1945                         
1946                         sock.Close ();
1947                         listen.Close ();
1948                 }
1949
1950                 [Test]
1951                 public void BeginConnectMultipleNull ()
1952                 {
1953                         Socket sock = new Socket (AddressFamily.InterNetwork,
1954                                                   SocketType.Stream,
1955                                                   ProtocolType.Tcp);
1956                         IPAddress[] ips = null;
1957                         
1958                         try {
1959                                 sock.BeginConnect (ips, 1246, BCCallback,
1960                                                    sock);
1961                                 Assert.Fail ("BeginConnectMultipleNull #1");
1962                         } catch (ArgumentNullException) {
1963                         } catch {
1964                                 Assert.Fail ("BeginConnectMultipleNull #2");
1965                         } finally {
1966                                 sock.Close ();
1967                         }
1968                 }
1969
1970                 [Test]
1971                 public void BeginConnectMultipleListen ()
1972                 {
1973                         Socket sock = new Socket (AddressFamily.InterNetwork,
1974                                                   SocketType.Stream,
1975                                                   ProtocolType.Tcp);
1976                         IPAddress[] ips = new IPAddress[4];
1977                         IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
1978                                                         1247);
1979                         
1980                         ips[0] = IPAddress.Parse ("127.0.0.4");
1981                         ips[1] = IPAddress.Parse ("127.0.0.3");
1982                         ips[2] = IPAddress.Parse ("127.0.0.2");
1983                         ips[3] = IPAddress.Parse ("127.0.0.1");
1984                         
1985                         sock.Bind (ep);
1986                         sock.Listen (1);
1987                         
1988                         try {
1989                                 sock.BeginConnect (ips, 1247, BCCallback,
1990                                                    sock);
1991                                 Assert.Fail ("BeginConnectMultipleListen #1");
1992                         } catch (InvalidOperationException) {
1993                         } catch {
1994                                 Assert.Fail ("BeginConnectMultipleListen #2");
1995                         } finally {
1996                                 sock.Close ();
1997                         }
1998                 }
1999                 
2000                 [Test]
2001                 [ExpectedException (typeof(ObjectDisposedException))]
2002                 public void BeginConnectMultipleClosed ()
2003                 {
2004                         Socket sock = new Socket (AddressFamily.InterNetwork,
2005                                                   SocketType.Stream,
2006                                                   ProtocolType.Tcp);
2007                         IPAddress[] ips = new IPAddress[4];
2008                         
2009                         ips[0] = IPAddress.Parse ("127.0.0.4");
2010                         ips[1] = IPAddress.Parse ("127.0.0.3");
2011                         ips[2] = IPAddress.Parse ("127.0.0.2");
2012                         ips[3] = IPAddress.Parse ("127.0.0.1");
2013                         
2014                         sock.Close ();
2015                         
2016                         sock.BeginConnect (ips, 1247, BCCallback, sock);
2017                 }
2018                 
2019                 [Test]
2020                 public void BeginConnectHostPortNull ()
2021                 {
2022                         Socket sock = new Socket (AddressFamily.InterNetwork,
2023                                                   SocketType.Stream,
2024                                                   ProtocolType.Tcp);
2025                         
2026                         try {
2027                                 sock.BeginConnect ((string)null, 0,
2028                                                    BCCallback, sock);
2029                                 Assert.Fail ("BeginConnectHostPort #1");
2030                         } catch (ArgumentNullException) {
2031                         } catch {
2032                                 Assert.Fail ("BeginConnectHostPort #2");
2033                         } finally {
2034                                 sock.Close ();
2035                         }
2036                 }
2037
2038                 [Test]
2039                 public void BeginConnectHostPortListen ()
2040                 {
2041                         Socket sock = new Socket (AddressFamily.InterNetwork,
2042                                                   SocketType.Stream,
2043                                                   ProtocolType.Tcp);
2044                         IPAddress ip = IPAddress.Loopback;
2045                         IPEndPoint ep = new IPEndPoint (ip, 1248);
2046                         
2047                         sock.Bind (ep);
2048                         sock.Listen (1);
2049                         
2050                         try {
2051                                 sock.BeginConnect ("localhost", 1248,
2052                                                    BCCallback, sock);
2053                                 Assert.Fail ("BeginConnectHostPortListen #1");
2054                         } catch (InvalidOperationException) {
2055                         } catch {
2056                                 Assert.Fail ("BeginConnectHostPortListen #2");
2057                         } finally {
2058                                 sock.Close ();
2059                         }
2060                 }
2061
2062                 [Test]
2063                 [Category ("NotWorking")] // Need to pick a non-IP AddressFamily that "works" on both mono and ms, this one only works on ms
2064                 public void BeginConnectHostPortNotIP ()
2065                 {
2066                         Socket sock = new Socket (AddressFamily.NetBios,
2067                                                   SocketType.Seqpacket,
2068                                                   ProtocolType.Unspecified);
2069                         
2070                         try {
2071                                 sock.BeginConnect ("localhost", 0, BCCallback,
2072                                                    sock);
2073                                 Assert.Fail ("BeginConnectHostPortNotIP #1");
2074                         } catch (NotSupportedException) {
2075                         } catch {
2076                                 Assert.Fail ("BeginConnectHostPortNotIP #2");
2077                         } finally {
2078                                 sock.Close ();
2079                         }
2080                 }
2081
2082                 [Test]
2083                 [ExpectedException (typeof(ObjectDisposedException))]
2084                 public void BeginConnectHostPortClosed ()
2085                 {
2086                         Socket sock = new Socket (AddressFamily.InterNetwork,
2087                                                   SocketType.Stream,
2088                                                   ProtocolType.Tcp);
2089                         
2090                         sock.Close ();
2091                         
2092                         sock.BeginConnect ("localhost", 0, BCCallback, sock);
2093                 }
2094                 
2095                 static bool BDDisconnected = false;
2096                 static ManualResetEvent BDCalledBack = new ManualResetEvent (false);
2097                 
2098                 private static void BDCallback (IAsyncResult asyncResult)
2099                 {
2100                         Socket sock = (Socket)asyncResult.AsyncState;
2101                         
2102                         sock.EndDisconnect (asyncResult);
2103                         BDDisconnected = true;
2104                         
2105                         BDCalledBack.Set ();
2106                 }
2107                 
2108                 [Test]
2109                 [Category ("NotDotNet")] // "Needs XP or later"
2110                 public void BeginDisconnect ()
2111                 {
2112                         Socket sock = new Socket (AddressFamily.InterNetwork,
2113                                                   SocketType.Stream,
2114                                                   ProtocolType.Tcp);
2115                         Socket listen = new Socket (AddressFamily.InterNetwork,
2116                                                     SocketType.Stream,
2117                                                     ProtocolType.Tcp);
2118                         IPAddress ip = IPAddress.Loopback;
2119                         IPEndPoint ep = new IPEndPoint (ip, 1254);
2120                         
2121                         listen.Bind (ep);
2122                         listen.Listen (1);
2123                         
2124                         sock.Connect (ip, 1254);
2125                         
2126                         Assertion.AssertEquals ("BeginDisconnect #1", true,
2127                                                 sock.Connected);
2128                         
2129                         sock.Shutdown (SocketShutdown.Both);
2130
2131                         BDCalledBack.Reset ();
2132                         BDDisconnected = false;
2133                         
2134                         sock.BeginDisconnect (false, BDCallback, sock);
2135                 
2136                         if (BDCalledBack.WaitOne (2000, false) == false) {
2137                                 Assert.Fail ("BeginDisconnect wait timed out");
2138                         }
2139                         
2140                         Assertion.AssertEquals ("BeginDisconnect #2", true,
2141                                                 BDDisconnected);
2142                         Assertion.AssertEquals ("BeginDisconnect #3", false,
2143                                                 sock.Connected);
2144                         
2145                         sock.Close ();
2146                         listen.Close ();
2147                 }
2148                 
2149                 [Test]
2150                 public void BeginReceiveSocketError ()
2151                 {
2152                 }
2153                 
2154                 [Test]
2155                 public void BeginReceiveGeneric ()
2156                 {
2157                 }
2158                 
2159                 [Test]
2160                 public void BeginReceiveGenericSocketError ()
2161                 {
2162                 }
2163                 
2164                 private static void BSCallback (IAsyncResult asyncResult)
2165                 {
2166                         Socket sock = (Socket)asyncResult.AsyncState;
2167                         
2168                         sock.EndSend (asyncResult);
2169                 }
2170                 
2171                 [Test]
2172                 public void BeginSendNotConnected ()
2173                 {
2174                         Socket sock = new Socket (AddressFamily.InterNetwork,
2175                                                   SocketType.Stream,
2176                                                   ProtocolType.Tcp);
2177                         
2178                         byte[] send_bytes = new byte[] {10, 11, 12, 13};
2179                         
2180                         try {
2181                                 sock.BeginSend (send_bytes, 0,
2182                                                 send_bytes.Length,
2183                                                 SocketFlags.None, BSCallback,
2184                                                 sock);
2185                                 Assert.Fail ("BeginSendNotConnected #1");
2186                         } catch (SocketException ex) {
2187                                 Assertion.AssertEquals ("BeginSendNotConnected #2", 10057, ex.ErrorCode);
2188                         } catch {
2189                                 Assert.Fail ("BeginSendNotConnected #3");
2190                         } finally {
2191                                 sock.Close ();
2192                         }
2193                 }
2194                 
2195                 [Test]
2196                 public void BeginSendSocketError ()
2197                 {
2198                 }
2199                 
2200                 [Test]
2201                 public void BeginSendGeneric ()
2202                 {
2203                 }
2204                 
2205                 [Test]
2206                 public void BeginSendGenericSocketError ()
2207                 {
2208                 }
2209                 
2210                 [Test]
2211                 public void BindTwice ()
2212                 {
2213                         Socket sock = new Socket (AddressFamily.InterNetwork,
2214                                                   SocketType.Stream,
2215                                                   ProtocolType.Tcp);
2216                         IPEndPoint ep1 = new IPEndPoint (IPAddress.Loopback,
2217                                                         1256);
2218                         IPEndPoint ep2 = new IPEndPoint (IPAddress.Loopback,
2219                                                          1257);
2220                         
2221                         sock.Bind (ep1);
2222                         
2223                         try {
2224                                 sock.Bind (ep2);
2225                                 Assert.Fail ("BindTwice #1");
2226                         } catch (SocketException ex) {
2227                                 Assertion.AssertEquals ("BindTwice #2",
2228                                                         10022, ex.ErrorCode);
2229                         } catch {
2230                                 Assert.Fail ("BindTwice #3");
2231                         } finally {
2232                                 sock.Close ();
2233                         }
2234                 }
2235                 
2236                 [Test]
2237                 public void Close ()
2238                 {
2239                         Socket sock = new Socket (AddressFamily.InterNetwork,
2240                                                   SocketType.Stream,
2241                                                   ProtocolType.Tcp);
2242                         Socket listen = new Socket (AddressFamily.InterNetwork,
2243                                                     SocketType.Stream,
2244                                                     ProtocolType.Tcp);
2245                         IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
2246                                                         1258);
2247                         
2248                         listen.Bind (ep);
2249                         listen.Listen (1);
2250                         
2251                         sock.Connect (ep);
2252
2253                         Assertion.AssertEquals ("Close #1", true,
2254                                                 sock.Connected);
2255                         
2256                         sock.Close (2);
2257                         
2258                         Thread.Sleep (3000);
2259                         
2260                         Assertion.AssertEquals ("Close #2", false,
2261                                                 sock.Connected);
2262                         
2263                         listen.Close ();
2264                 }
2265                 
2266                 [Test]
2267                 public void ConnectAddressPort ()
2268                 {
2269                         Socket sock = new Socket (AddressFamily.InterNetwork,
2270                                                   SocketType.Stream,
2271                                                   ProtocolType.Tcp);
2272                         Socket listen = new Socket (AddressFamily.InterNetwork,
2273                                                     SocketType.Stream,
2274                                                     ProtocolType.Tcp);
2275                         IPAddress ip = IPAddress.Loopback;
2276                         IPEndPoint ep = new IPEndPoint (ip, 1249);
2277
2278                         listen.Bind (ep);
2279                         listen.Listen (1);
2280                         
2281                         sock.Connect (ip, 1249);
2282                         
2283                         Assertion.AssertEquals ("ConnectAddressPort #1",
2284                                                 true, sock.Connected);
2285                         
2286                         sock.Close ();
2287                         listen.Close ();
2288                 }
2289
2290                 [Test]
2291                 public void ConnectAddressPortNull ()
2292                 {
2293                         Socket sock = new Socket (AddressFamily.InterNetwork,
2294                                                   SocketType.Stream,
2295                                                   ProtocolType.Tcp);
2296                         IPAddress ip = null;
2297
2298                         try {
2299                                 sock.Connect (ip, 1249);
2300                                 Assert.Fail ("ConnectAddressPortNull #1");
2301                         } catch (ArgumentNullException) {
2302                         } catch {
2303                                 Assert.Fail ("ConnectAddressPortNull #2");
2304                         } finally {
2305                                 sock.Close ();
2306                         }
2307                 }
2308
2309                 [Test]
2310                 public void ConnectAddressPortListen ()
2311                 {
2312                         Socket sock = new Socket (AddressFamily.InterNetwork,
2313                                                   SocketType.Stream,
2314                                                   ProtocolType.Tcp);
2315                         IPAddress ip = IPAddress.Loopback;
2316                         IPEndPoint ep = new IPEndPoint (ip, 1250);
2317
2318                         sock.Bind (ep);
2319                         sock.Listen (1);
2320                         
2321                         try {
2322                                 sock.Connect (ip, 1250);
2323                                 Assert.Fail ("ConnectAddressPortListen #1");
2324                         } catch (InvalidOperationException) {
2325                         } catch {
2326                                 Assert.Fail ("ConnectAddressPortListen #2");
2327                         } finally {
2328                                 sock.Close ();
2329                         }
2330                 }
2331                 
2332                 [Test]
2333                 [ExpectedException (typeof(ObjectDisposedException))]
2334                 public void ConnectAddressPortClosed ()
2335                 {
2336                         Socket sock = new Socket (AddressFamily.InterNetwork,
2337                                                   SocketType.Stream,
2338                                                   ProtocolType.Tcp);
2339                         IPAddress ip = IPAddress.Loopback;
2340                         
2341                         sock.Close ();
2342                         
2343                         sock.Connect (ip, 1250);
2344                 }
2345                 
2346                 [Test]
2347                 [Category ("NotOnMac")] // MacOSX trashes the fd after the failed connect attempt to 127.0.0.4
2348                 public void ConnectMultiple ()
2349                 {
2350                         Socket sock = new Socket (AddressFamily.InterNetwork,
2351                                                   SocketType.Stream,
2352                                                   ProtocolType.Tcp);
2353                         Socket listen = new Socket (AddressFamily.InterNetwork,
2354                                                     SocketType.Stream,
2355                                                     ProtocolType.Tcp);
2356                         IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
2357                                                         1251);
2358                         IPAddress[] ips = new IPAddress[4];
2359                         
2360                         ips[0] = IPAddress.Parse ("127.0.0.4");
2361                         ips[1] = IPAddress.Parse ("127.0.0.3");
2362                         ips[2] = IPAddress.Parse ("127.0.0.2");
2363                         ips[3] = IPAddress.Parse ("127.0.0.1");
2364
2365                         listen.Bind (ep);
2366                         listen.Listen (1);
2367                         
2368                         sock.Connect (ips, 1251);
2369                         
2370                         Assertion.AssertEquals ("ConnectMultiple #1",
2371                                                 true, sock.Connected);
2372                         Assertion.AssertEquals ("ConnectMultiple #2",
2373                                                 AddressFamily.InterNetwork,
2374                                                 sock.RemoteEndPoint.AddressFamily);
2375                         IPEndPoint remep = (IPEndPoint)sock.RemoteEndPoint;
2376                         
2377                         Assertion.AssertEquals ("ConnectMultiple #2",
2378                                                 IPAddress.Loopback,
2379                                                 remep.Address);
2380                         
2381                         sock.Close ();
2382                         listen.Close ();
2383                 }
2384
2385                 [Test]
2386                 public void ConnectMultipleNull ()
2387                 {
2388                         Socket sock = new Socket (AddressFamily.InterNetwork,
2389                                                   SocketType.Stream,
2390                                                   ProtocolType.Tcp);
2391                         IPAddress[] ips = null;
2392                         
2393                         try {
2394                                 sock.Connect (ips, 1251);
2395                                 Assert.Fail ("ConnectMultipleNull #1");
2396                         } catch (ArgumentNullException) {
2397                         } catch {
2398                                 Assert.Fail ("ConnectMultipleNull #2");
2399                         } finally {
2400                                 sock.Close ();
2401                         }
2402                 }
2403
2404                 [Test]
2405                 public void ConnectMultipleListen ()
2406                 {
2407                         Socket sock = new Socket (AddressFamily.InterNetwork,
2408                                                   SocketType.Stream,
2409                                                   ProtocolType.Tcp);
2410                         IPAddress[] ips = new IPAddress[4];
2411                         IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
2412                                                         1252);
2413                         
2414                         ips[0] = IPAddress.Parse ("127.0.0.4");
2415                         ips[1] = IPAddress.Parse ("127.0.0.3");
2416                         ips[2] = IPAddress.Parse ("127.0.0.2");
2417                         ips[3] = IPAddress.Parse ("127.0.0.1");
2418                         
2419                         sock.Bind (ep);
2420                         sock.Listen (1);
2421                         
2422                         try {
2423                                 sock.Connect (ips, 1252);
2424                                 Assert.Fail ("ConnectMultipleListen #1");
2425                         } catch (InvalidOperationException) {
2426                         } catch {
2427                                 Assert.Fail ("ConnectMultipleListen #2");
2428                         } finally {
2429                                 sock.Close ();
2430                         }
2431                 }
2432                 
2433                 [Test]
2434                 [ExpectedException (typeof(ObjectDisposedException))]
2435                 public void ConnectMultipleClosed ()
2436                 {
2437                         Socket sock = new Socket (AddressFamily.InterNetwork,
2438                                                   SocketType.Stream,
2439                                                   ProtocolType.Tcp);
2440                         IPAddress[] ips = new IPAddress[4];
2441                         
2442                         ips[0] = IPAddress.Parse ("127.0.0.4");
2443                         ips[1] = IPAddress.Parse ("127.0.0.3");
2444                         ips[2] = IPAddress.Parse ("127.0.0.2");
2445                         ips[3] = IPAddress.Parse ("127.0.0.1");
2446                         
2447                         sock.Close ();
2448                         
2449                         sock.Connect (ips, 1252);
2450                 }
2451                 
2452                 [Test]
2453                 public void ConnectHostPortNull ()
2454                 {
2455                         Socket sock = new Socket (AddressFamily.InterNetwork,
2456                                                   SocketType.Stream,
2457                                                   ProtocolType.Tcp);
2458                         
2459                         try {
2460                                 sock.Connect ((string)null, 0);
2461                                 Assert.Fail ("ConnectHostPort #1");
2462                         } catch (ArgumentNullException) {
2463                         } catch {
2464                                 Assert.Fail ("ConnectHostPort #2");
2465                         } finally {
2466                                 sock.Close ();
2467                         }
2468                 }
2469
2470                 [Test]
2471                 public void ConnectHostPortListen ()
2472                 {
2473                         Socket sock = new Socket (AddressFamily.InterNetwork,
2474                                                   SocketType.Stream,
2475                                                   ProtocolType.Tcp);
2476                         IPAddress ip = IPAddress.Loopback;
2477                         IPEndPoint ep = new IPEndPoint (ip, 1253);
2478                         
2479                         sock.Bind (ep);
2480                         sock.Listen (1);
2481                         
2482                         try {
2483                                 sock.Connect ("localhost", 1253);
2484                                 Assert.Fail ("ConnectHostPortListen #1");
2485                         } catch (InvalidOperationException) {
2486                         } catch {
2487                                 Assert.Fail ("ConnectHostPortListen #2");
2488                         } finally {
2489                                 sock.Close ();
2490                         }
2491                 }
2492
2493                 [Test]
2494                 [Category ("NotWorking")] // Need to pick a non-IP AddressFamily that "works" on both mono and ms, this one only works on ms
2495                 public void ConnectHostPortNotIP ()
2496                 {
2497                         Socket sock = new Socket (AddressFamily.NetBios,
2498                                                   SocketType.Seqpacket,
2499                                                   ProtocolType.Unspecified);
2500                         
2501                         try {
2502                                 sock.Connect ("localhost", 0);
2503                                 Assert.Fail ("ConnectHostPortNotIP #1");
2504                         } catch (NotSupportedException) {
2505                         } catch {
2506                                 Assert.Fail ("ConnectHostPortNotIP #2");
2507                         } finally {
2508                                 sock.Close ();
2509                         }
2510                 }
2511
2512                 [Test]
2513                 [ExpectedException (typeof(ObjectDisposedException))]
2514                 public void ConnectHostPortClosed ()
2515                 {
2516                         Socket sock = new Socket (AddressFamily.InterNetwork,
2517                                                   SocketType.Stream,
2518                                                   ProtocolType.Tcp);
2519                         
2520                         sock.Close ();
2521                         
2522                         sock.Connect ("localhost", 0);
2523                 }
2524                 
2525                 [Test]
2526                 [Category ("NotDotNet")] // "Needs XP or later"
2527                 public void Disconnect ()
2528                 {
2529                         Socket sock = new Socket (AddressFamily.InterNetwork,
2530                                                   SocketType.Stream,
2531                                                   ProtocolType.Tcp);
2532                         Socket listen = new Socket (AddressFamily.InterNetwork,
2533                                                     SocketType.Stream,
2534                                                     ProtocolType.Tcp);
2535                         IPAddress ip = IPAddress.Loopback;
2536                         IPEndPoint ep = new IPEndPoint (ip, 1255);
2537                         
2538                         listen.Bind (ep);
2539                         listen.Listen (1);
2540                         
2541                         sock.Connect (ip, 1255);
2542                         
2543                         Assertion.AssertEquals ("Disconnect #1", true,
2544                                                 sock.Connected);
2545                         
2546                         sock.Shutdown (SocketShutdown.Both);
2547
2548                         sock.Disconnect (false);
2549
2550                         Assertion.AssertEquals ("BeginDisconnect #3", false,
2551                                                 sock.Connected);
2552                         
2553                         sock.Close ();
2554                         listen.Close ();
2555                 }
2556                 
2557                 [Test]
2558                 public void DuplicateAndClose ()
2559                 {
2560                 }
2561                 
2562                 [Test]
2563                 public void IOControl ()
2564                 {
2565                 }
2566                 
2567                 [Test]
2568                 public void ReceiveGeneric ()
2569                 {
2570                         int i;
2571
2572                         IPEndPoint endpoint = new IPEndPoint(IPAddress.Loopback, 1258);
2573
2574                         Socket listensock = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
2575                         listensock.Bind (endpoint);
2576                         listensock.Listen(1);
2577
2578                         Socket sendsock = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
2579                         sendsock.Connect(endpoint);
2580
2581                         Socket clientsock = listensock.Accept();
2582                         
2583                         byte[] sendbuf = new byte[256];
2584
2585                         for(i = 0; i < 256; i++) {
2586                                 sendbuf[i] = (byte)i;
2587                         }
2588                         for (i = 4; i < 6; i++) {
2589                                 Assert.AreEqual (sendbuf[i], (byte)i,
2590                                                  "#1/" + i.ToString());
2591                         }
2592
2593                         SocketError err;
2594                         sendsock.Send (sendbuf, 0, 256, SocketFlags.None,
2595                                        out err);
2596
2597
2598                         byte[] recvbuf = new byte[256];
2599                         List<ArraySegment<byte>> recvbuflist = new List<ArraySegment<byte>>(2);
2600                         recvbuflist.Add(new ArraySegment<byte>(recvbuf, 4, 2));
2601                         recvbuflist.Add(new ArraySegment<byte>(recvbuf, 20, 230));
2602                         
2603                         clientsock.Receive (recvbuflist);
2604
2605                         /* recvbuf should now hold the first 2 bytes
2606                          * of sendbuf from pos 4, and the next 230
2607                          * bytes of sendbuf from pos 20
2608                          */
2609
2610                         for (i = 0; i < 2; i++) {
2611                                 Assert.AreEqual (sendbuf[i], recvbuf[i + 4],
2612                                                  "#2/" + i.ToString());
2613                         }
2614                         for (i = 2; i < 232; i++) {
2615                                 Assert.AreEqual (sendbuf[i], recvbuf[i + 18],
2616                                                  "#2/" + i.ToString());
2617                         }
2618
2619                         sendsock.Close ();
2620                         clientsock.Close ();
2621                         listensock.Close ();
2622                 }
2623                 
2624                 [Test]
2625                 public void SendGeneric ()
2626                 {
2627                         int i;
2628
2629                         IPEndPoint endpoint = new IPEndPoint(IPAddress.Loopback, 1259);
2630
2631                         Socket listensock = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
2632                         listensock.Bind (endpoint);
2633                         listensock.Listen(1);
2634
2635                         Socket sendsock = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
2636                         sendsock.Connect(endpoint);
2637
2638                         Socket clientsock = listensock.Accept();
2639
2640                         byte[] sendbuf = new byte[256];
2641                         List<ArraySegment<byte>> sendbuflist = new List<ArraySegment<byte>>(2);
2642
2643                         sendbuflist.Add(new ArraySegment<byte>(sendbuf, 4, 2));
2644                         sendbuflist.Add(new ArraySegment<byte>(sendbuf, 20, 230));
2645
2646                         for(i = 0; i < 256; i++) {
2647                                 sendbuf[i] = (byte)i;
2648                         }
2649                         for (i = 4; i < 6; i++) {
2650                                 Assert.AreEqual (sendbuf[i], (byte)i,
2651                                                  "#1/" + i.ToString());
2652                         }
2653
2654                         SocketError err;
2655                         sendsock.Send (sendbuflist, SocketFlags.None, out err);
2656
2657                         
2658                         byte[] recvbuf = new byte[256];
2659
2660                         clientsock.Receive (recvbuf);
2661
2662                         /* The first 2 bytes of recvbuf should now
2663                          * hold 2 bytes of sendbuf from pos 4, and the
2664                          * next 230 bytes of recvbuf should be sendbuf
2665                          * from pos 20
2666                          */
2667
2668                         for (i = 0; i < 2; i++) {
2669                                 Assert.AreEqual (recvbuf[i], sendbuf[i + 4],
2670                                                  "#2/" + i.ToString());
2671                         }
2672                         for (i = 2; i < 232; i++) {
2673                                 Assert.AreEqual (recvbuf[i], sendbuf[i + 18],
2674                                                  "#2/" + i.ToString());
2675                         }
2676
2677                         sendsock.Close ();
2678                         clientsock.Close ();
2679                         listensock.Close ();
2680                 }
2681
2682                 [Test]
2683                 public void ListenNotBound ()
2684                 {
2685                         Socket sock = new Socket (AddressFamily.InterNetwork,
2686                                                   SocketType.Stream,
2687                                                   ProtocolType.Tcp);
2688                         
2689                         try {
2690                                 sock.Listen (1);
2691                                 Assert.Fail ("ListenNotBound #1");
2692                         } catch (SocketException ex) {
2693                                 Assertion.AssertEquals ("ListenNotBound #2",
2694                                                         10022, ex.ErrorCode);
2695                         } catch {
2696                                 Assert.Fail ("ListenNotBound #3");
2697                         } finally {
2698                                 sock.Close ();
2699                         }
2700                 }
2701 #endif
2702
2703                 static Socket CWRSocket;
2704                 static bool CWRReceiving = true;
2705                 static ManualResetEvent CWRReady = new ManualResetEvent (false);
2706                 
2707                 private static void CWRReceiveThread ()
2708                 {
2709                         byte[] buf = new byte[256];
2710                         
2711                         try {
2712                                 CWRSocket.Receive (buf);
2713                         } catch (SocketException) {
2714                                 CWRReceiving = false;
2715                         }
2716
2717                         CWRReady.Set ();
2718                 }
2719                 
2720                 [Test]
2721                 public void CloseWhileReceiving ()
2722                 {
2723                         CWRSocket = new Socket (AddressFamily.InterNetwork,
2724                                                 SocketType.Dgram,
2725                                                 ProtocolType.Udp);
2726                         CWRSocket.Bind (new IPEndPoint (IPAddress.Loopback,
2727                                                         1256));
2728                         
2729                         Thread recv_thread = new Thread (new ThreadStart (CWRReceiveThread));
2730                         CWRReady.Reset ();
2731                         recv_thread.Start ();
2732                         Thread.Sleep (250);     /* Wait for the thread to be already receiving */
2733
2734                         CWRSocket.Close ();
2735                         if (CWRReady.WaitOne (1000, false) == false) {
2736                                 Assert.Fail ("CloseWhileReceiving wait timed out");
2737                         }
2738                         
2739                         Assert.IsFalse (CWRReceiving);
2740                 }
2741
2742                 static bool RRCLastRead = false;
2743                 static ManualResetEvent RRCReady = new ManualResetEvent (false);
2744                 
2745                 private static void RRCClientThread ()
2746                 {
2747                         byte[] bytes = new byte[8];
2748                         int readbyte;
2749                         
2750                         Socket sock = new Socket (AddressFamily.InterNetwork,
2751                                                   SocketType.Stream,
2752                                                   ProtocolType.Tcp);
2753                         sock.Connect (new IPEndPoint (IPAddress.Loopback,
2754                                                       1257));
2755                         
2756                         NetworkStream stream = new NetworkStream (sock);
2757
2758                         readbyte = stream.ReadByte ();
2759                         Assertion.AssertEquals ("ReceiveRemoteClosed #1",
2760                                                 0, readbyte);
2761                         
2762                         stream.Read (bytes, 0, 0);
2763
2764                         readbyte = stream.ReadByte ();
2765                         Assertion.AssertEquals ("ReceiveRemoteClosed #2",
2766                                                 0, readbyte);
2767                         
2768                         stream.Read (bytes, 0, 0);
2769
2770                         readbyte = stream.ReadByte ();
2771                         Assertion.AssertEquals ("ReceiveRemoteClosed #3",
2772                                                 -1, readbyte);
2773
2774                         sock.Close ();
2775
2776                         RRCLastRead = true;
2777                         RRCReady.Set ();
2778                 }
2779                 
2780                 [Test]
2781                 public void ReceiveRemoteClosed ()
2782                 {
2783                         Socket sock = new Socket (AddressFamily.InterNetwork,
2784                                                   SocketType.Stream,
2785                                                   ProtocolType.Tcp);
2786                         sock.Bind (new IPEndPoint (IPAddress.Loopback, 1257));
2787                         sock.Listen (1);
2788                         
2789                         RRCReady.Reset ();
2790                         Thread client_thread = new Thread (new ThreadStart (RRCClientThread));
2791                         client_thread.Start ();
2792                         
2793                         Socket client = sock.Accept ();
2794                         NetworkStream stream = new NetworkStream (client);
2795                         stream.WriteByte (0x00);
2796                         stream.WriteByte (0x00);
2797                         client.Close ();
2798                         sock.Close ();
2799
2800                         RRCReady.WaitOne (1000, false);
2801                         Assert.IsTrue (RRCLastRead);
2802                 }
2803
2804                 //
2805                 // Test case for bug #471580
2806                 [Test]
2807                 public void UdpDoubleBind ()
2808                 {
2809                         Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
2810                         s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, 1);
2811                         
2812                         s.Bind (new IPEndPoint (IPAddress.Any, 12345));
2813                         
2814                         Socket ss = new Socket (AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
2815                         ss.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, 1);
2816                         
2817                         ss.Bind (new IPEndPoint (IPAddress.Any, 12345));
2818
2819                         // If we make it this far, we succeeded.
2820                         
2821                         ss.Close ();
2822                         s.Close ();
2823                 }
2824                 
2825 #if NET_2_0
2826                 [Test]
2827                 public void ConnectedProperty ()
2828                 {
2829                         TcpListener listener = new TcpListener (IPAddress.Loopback, 23456);
2830                         listener.Start();
2831
2832                         Socket client = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
2833                         client.Connect (IPAddress.Loopback, 23456);
2834                         Socket server = listener.AcceptSocket ();
2835
2836                         try {
2837                                 server.EndSend(server.BeginSend (new byte[10], 0, 10, SocketFlags.None, null, null));
2838                                 client.Close ();
2839                                 try {
2840                                         server.EndReceive (server.BeginReceive (new byte[10], 0, 10, SocketFlags.None, null, null));
2841                                 } catch {
2842                                 }
2843                                 Assert.IsTrue (!client.Connected);
2844                                 Assert.IsTrue (!server.Connected);
2845                         } finally {
2846                                 listener.Stop ();
2847                                 client.Close ();
2848                                 server.Close ();
2849                         }
2850                 }
2851 #endif
2852         }
2853 }
2854