PageRenderTime 166ms CodeModel.GetById 3ms app.highlight 141ms RepoModel.GetById 1ms app.codeStats 1ms

/mcs/class/System/Test/System.Net.Sockets/SocketTest.cs

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