[socket] Factor SocketAsyncResult and ProcessAsyncResult common parts into IOAsyncRes...
[mono.git] / mcs / class / System / System.Net.Sockets / Socket.cs
1 // System.Net.Sockets.Socket.cs
2 //
3 // Authors:
4 //      Phillip Pearson (pp@myelin.co.nz)
5 //      Dick Porter <dick@ximian.com>
6 //      Gonzalo Paniagua Javier (gonzalo@ximian.com)
7 //      Sridhar Kulkarni (sridharkulkarni@gmail.com)
8 //      Brian Nickel (brian.nickel@gmail.com)
9 //      Ludovic Henry (ludovic@xamarin.com)
10 //
11 // Copyright (C) 2001, 2002 Phillip Pearson and Ximian, Inc.
12 //    http://www.myelin.co.nz
13 // (c) 2004-2011 Novell, Inc. (http://www.novell.com)
14 //
15 //
16 // Permission is hereby granted, free of charge, to any person obtaining
17 // a copy of this software and associated documentation files (the
18 // "Software"), to deal in the Software without restriction, including
19 // without limitation the rights to use, copy, modify, merge, publish,
20 // distribute, sublicense, and/or sell copies of the Software, and to
21 // permit persons to whom the Software is furnished to do so, subject to
22 // the following conditions:
23 // 
24 // The above copyright notice and this permission notice shall be
25 // included in all copies or substantial portions of the Software.
26 // 
27 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
28 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
29 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
30 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
31 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
32 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
33 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 //
35
36 using System;
37 using System.Net;
38 using System.Collections;
39 using System.Collections.Generic;
40 using System.Runtime.CompilerServices;
41 using System.Runtime.InteropServices;
42 using System.Threading;
43 using System.Reflection;
44 using System.IO;
45 using System.Net.Configuration;
46 using System.Text;
47 using System.Timers;
48 using System.Net.NetworkInformation;
49
50 namespace System.Net.Sockets 
51 {
52         public partial class Socket : IDisposable
53         {
54                 const int SOCKET_CLOSED_CODE = 10004;
55                 const string TIMEOUT_EXCEPTION_MSG = "A connection attempt failed because the connected party did not properly respond" +
56                         "after a period of time, or established connection failed because connected host has failed to respond";
57
58                 /*
59                  *      These two fields are looked up by name by the runtime, don't change
60                  *  their name without also updating the runtime code.
61                  */
62                 static int ipv4_supported = -1;
63                 static int ipv6_supported = -1;
64
65                 /* true if we called Close_internal */
66                 bool is_closed;
67
68                 bool is_listening;
69                 bool use_overlapped_io;
70
71                 int linger_timeout;
72
73                 AddressFamily address_family;
74                 SocketType socket_type;
75                 ProtocolType protocol_type;
76
77                 /* the field "safe_handle" is looked up by name by the runtime */
78                 internal SafeSocketHandle safe_handle;
79
80                 /*
81                  * This EndPoint is used when creating new endpoints. Because
82                  * there are many types of EndPoints possible,
83                  * seed_endpoint.Create(addr) is used for creating new ones.
84                  * As such, this value is set on Bind, SentTo, ReceiveFrom,
85                  * Connect, etc.
86                  */
87                 internal EndPoint seed_endpoint = null;
88
89                 internal Queue<KeyValuePair<IntPtr, IOSelectorJob>> readQ = new Queue<KeyValuePair<IntPtr, IOSelectorJob>> (2);
90                 internal Queue<KeyValuePair<IntPtr, IOSelectorJob>> writeQ = new Queue<KeyValuePair<IntPtr, IOSelectorJob>> (2);
91
92                 internal bool is_blocking = true;
93                 internal bool is_bound;
94
95                 /* When true, the socket was connected at the time of the last IO operation */
96                 internal bool is_connected;
97
98                 internal bool is_disposed;
99                 internal bool connect_in_progress;
100
101 #region Constructors
102
103                 static Socket ()
104                 {
105                         if (ipv4_supported == -1) {
106                                 try {
107                                         Socket tmp = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
108                                         tmp.Close();
109
110                                         ipv4_supported = 1;
111                                 } catch {
112                                         ipv4_supported = 0;
113                                 }
114                         }
115
116                         if (ipv6_supported == -1) {
117                                 // We need to put a try/catch around ConfigurationManager methods as will always throw an exception 
118                                 // when run in a mono embedded application.  This occurs as embedded applications do not have a setup
119                                 // for application config.  The exception is not thrown when called from a normal .NET application. 
120                                 //
121                                 // We, then, need to guard calls to the ConfigurationManager.  If the config is not found or throws an
122                                 // exception, will fall through to the existing Socket / API directly below in the code.
123                                 //
124                                 // Also note that catching ConfigurationErrorsException specifically would require library dependency
125                                 // System.Configuration, and wanted to avoid that.
126 #if !NET_2_1
127 #if CONFIGURATION_DEP
128                                 try {
129                                         SettingsSection config;
130                                         config = (SettingsSection) System.Configuration.ConfigurationManager.GetSection ("system.net/settings");
131                                         if (config != null)
132                                                 ipv6_supported = config.Ipv6.Enabled ? -1 : 0;
133                                 } catch {
134                                         ipv6_supported = -1;
135                                 }
136 #else
137                                 try {
138                                         NetConfig config = System.Configuration.ConfigurationSettings.GetConfig("system.net/settings") as NetConfig;
139                                         if (config != null)
140                                                 ipv6_supported = config.ipv6Enabled ? -1 : 0;
141                                 } catch {
142                                         ipv6_supported = -1;
143                                 }
144 #endif
145 #endif
146                                 if (ipv6_supported != 0) {
147                                         try {
148                                                 Socket tmp = new Socket(AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp);
149                                                 tmp.Close();
150
151                                                 ipv6_supported = 1;
152                                         } catch {
153                                                 ipv6_supported = 0;
154                                         }
155                                 }
156                         }
157                 }
158
159                 //
160                 // This constructor is used by servers that want to listen for instance on both
161                 // ipv4 and ipv6.   Mono has historically done that if you use InterNetworkV6 (at
162                 // least on Unix), because that is the default behavior unless the IPV6_V6ONLY
163                 // option is explicitly set by using setsockopt (sock, IPPROTO_IPV6, IPV6_ONLY)
164                 //
165                 public Socket (SocketType socketType, ProtocolType protocolType)
166                         : this (AddressFamily.InterNetworkV6, socketType, protocolType)
167                 {
168                         DualMode = true;
169                 }
170                 
171                 public Socket(AddressFamily addressFamily, SocketType socketType, ProtocolType protocolType)
172                 {
173 #if NET_2_1 && !MOBILE
174                         switch (addressFamily) {
175                         case AddressFamily.InterNetwork:    // ok
176                         case AddressFamily.InterNetworkV6:  // ok
177                         case AddressFamily.Unknown:         // SocketException will be thrown later (with right error #)
178                                 break;
179                         // case AddressFamily.Unspecified:
180                         default:
181                                 throw new ArgumentException ("addressFamily");
182                         }
183
184                         switch (socketType) {
185                         case SocketType.Stream:             // ok
186                         case SocketType.Unknown:            // SocketException will be thrown later (with right error #)
187                                 break;
188                         default:
189                                 throw new ArgumentException ("socketType");
190                         }
191
192                         switch (protocolType) {
193                         case ProtocolType.Tcp:              // ok
194                         case ProtocolType.Unspecified:      // ok
195                         case ProtocolType.Unknown:          // SocketException will be thrown later (with right error #)
196                                 break;
197                         default:
198                                 throw new ArgumentException ("protocolType");
199                         }
200 #endif
201                         this.address_family = addressFamily;
202                         this.socket_type = socketType;
203                         this.protocol_type = protocolType;
204                         
205                         int error;
206                         var handle = Socket_internal (addressFamily, socketType, protocolType, out error);
207
208                         this.safe_handle = new SafeSocketHandle (handle, true);
209
210                         if (error != 0)
211                                 throw new SocketException (error);
212
213 #if !NET_2_1 || MOBILE
214                         SocketDefaults ();
215 #endif
216                 }
217
218 #if !MOBILE
219                 public Socket (SocketInformation socketInformation)
220                 {
221                         this.is_listening      = (socketInformation.Options & SocketInformationOptions.Listening) != 0;
222                         this.is_connected      = (socketInformation.Options & SocketInformationOptions.Connected) != 0;
223                         this.is_blocking       = (socketInformation.Options & SocketInformationOptions.NonBlocking) == 0;
224                         this.use_overlapped_io = (socketInformation.Options & SocketInformationOptions.UseOnlyOverlappedIO) != 0;
225
226                         var result = Mono.DataConverter.Unpack ("iiiil", socketInformation.ProtocolInformation, 0);
227
228                         this.address_family = (AddressFamily) (int) result [0];
229                         this.socket_type = (SocketType) (int) result [1];
230                         this.protocol_type = (ProtocolType) (int) result [2];
231                         this.is_bound = (ProtocolType) (int) result [3] != 0;
232                         this.safe_handle = new SafeSocketHandle ((IntPtr) (long) result [4], true);
233
234                         SocketDefaults ();
235                 }
236 #endif
237
238                 /* private constructor used by Accept, which already has a socket handle to use */
239                 internal Socket(AddressFamily family, SocketType type, ProtocolType proto, SafeSocketHandle safe_handle)
240                 {
241                         this.address_family = family;
242                         this.socket_type = type;
243                         this.protocol_type = proto;
244                         
245                         this.safe_handle = safe_handle;
246                         this.is_connected = true;
247                 }
248
249                 ~Socket ()
250                 {
251                         Dispose (false);
252                 }
253
254                 void SocketDefaults ()
255                 {
256                         try {
257                                 /* Need to test IPv6 further */
258                                 if (address_family == AddressFamily.InterNetwork
259                                         // || address_family == AddressFamily.InterNetworkV6
260                                 ) {
261                                         /* This is the default, but it probably has nasty side
262                                          * effects on Linux, as the socket option is kludged by
263                                          * turning on or off PMTU discovery... */
264                                         this.DontFragment = false;
265                                 }
266
267                                 /* Microsoft sets these to 8192, but we are going to keep them
268                                  * both to the OS defaults as these have a big performance impact.
269                                  * on WebClient performance. */
270                                 // this.ReceiveBufferSize = 8192;
271                                 // this.SendBufferSize = 8192;
272                         } catch (SocketException) {
273                         }
274                 }
275
276                 /* Creates a new system socket, returning the handle */
277                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
278                 extern IntPtr Socket_internal (AddressFamily family, SocketType type, ProtocolType proto, out int error);
279
280 #endregion
281
282 #region Properties
283
284                 [ObsoleteAttribute ("Use OSSupportsIPv4 instead")]
285                 public static bool SupportsIPv4 {
286                         get { return ipv4_supported == 1; }
287                 }
288
289                 [ObsoleteAttribute ("Use OSSupportsIPv6 instead")]
290                 public static bool SupportsIPv6 {
291                         get { return ipv6_supported == 1; }
292                 }
293
294 #if NET_2_1
295                 public static bool OSSupportsIPv4 {
296                         get { return ipv4_supported == 1; }
297                 }
298 #else
299                 public static bool OSSupportsIPv4 {
300                         get {
301                                 NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces ();
302
303                                 foreach (NetworkInterface adapter in nics) {
304                                         if (adapter.Supports (NetworkInterfaceComponent.IPv4))
305                                                 return true;
306                                 }
307
308                                 return false;
309                         }
310                 }
311 #endif
312
313 #if NET_2_1
314                 public static bool OSSupportsIPv6 {
315                         get { return ipv6_supported == 1; }
316                 }
317 #else
318                 public static bool OSSupportsIPv6 {
319                         get {
320                                 NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces ();
321
322                                 foreach (NetworkInterface adapter in nics) {
323                                         if (adapter.Supports (NetworkInterfaceComponent.IPv6))
324                                                 return true;
325                                 }
326
327                                 return false;
328                         }
329                 }
330 #endif
331
332                 public int Available {
333                         get {
334                                 ThrowIfDisposedAndClosed ();
335
336                                 int ret, error;
337                                 ret = Available_internal (safe_handle, out error);
338
339                                 if (error != 0)
340                                         throw new SocketException (error);
341
342                                 return ret;
343                         }
344                 }
345
346                 static int Available_internal (SafeSocketHandle safeHandle, out int error)
347                 {
348                         bool release = false;
349                         try {
350                                 safeHandle.DangerousAddRef (ref release);
351                                 return Available_internal (safeHandle.DangerousGetHandle (), out error);
352                         } finally {
353                                 if (release)
354                                         safeHandle.DangerousRelease ();
355                         }
356                 }
357
358                 /* Returns the amount of data waiting to be read on socket */
359                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
360                 extern static int Available_internal (IntPtr socket, out int error);
361
362                 public bool DontFragment {
363                         get {
364                                 ThrowIfDisposedAndClosed ();
365
366                                 switch (address_family) {
367                                 case AddressFamily.InterNetwork:
368                                         return ((int) GetSocketOption (SocketOptionLevel.IP, SocketOptionName.DontFragment)) != 0;
369                                 case AddressFamily.InterNetworkV6:
370                                         return ((int) GetSocketOption (SocketOptionLevel.IPv6, SocketOptionName.DontFragment)) != 0;
371                                 default:
372                                         throw new NotSupportedException ("This property is only valid for InterNetwork and InterNetworkV6 sockets");
373                                 }
374                         }
375                         set {
376                                 ThrowIfDisposedAndClosed ();
377
378                                 switch (address_family) {
379                                 case AddressFamily.InterNetwork:
380                                         SetSocketOption (SocketOptionLevel.IP, SocketOptionName.DontFragment, value ? 1 : 0);
381                                         break;
382                                 case AddressFamily.InterNetworkV6:
383                                         SetSocketOption (SocketOptionLevel.IPv6, SocketOptionName.DontFragment, value ? 1 : 0);
384                                         break;
385                                 default:
386                                         throw new NotSupportedException ("This property is only valid for InterNetwork and InterNetworkV6 sockets");
387                                 }
388                         }
389                 }
390
391                 public bool EnableBroadcast {
392                         get {
393                                 ThrowIfDisposedAndClosed ();
394
395                                 if (protocol_type != ProtocolType.Udp)
396                                         throw new SocketException ((int) SocketError.ProtocolOption);
397
398                                 return ((int) GetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Broadcast)) != 0;
399                         }
400                         set {
401                                 ThrowIfDisposedAndClosed ();
402
403                                 if (protocol_type != ProtocolType.Udp)
404                                         throw new SocketException ((int) SocketError.ProtocolOption);
405
406                                 SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Broadcast, value ? 1 : 0);
407                         }
408                 }
409
410                 public bool ExclusiveAddressUse {
411                         get {
412                                 ThrowIfDisposedAndClosed ();
413
414                                 return ((int) GetSocketOption (SocketOptionLevel.Socket, SocketOptionName.ExclusiveAddressUse)) != 0;
415                         }
416                         set {
417                                 ThrowIfDisposedAndClosed ();
418
419                                 if (is_bound)
420                                         throw new InvalidOperationException ("Bind has already been called for this socket");
421
422                                 SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.ExclusiveAddressUse, value ? 1 : 0);
423                         }
424                 }
425
426                 public bool IsBound {
427                         get {
428                                 return is_bound;
429                         }
430                 }
431
432                 public LingerOption LingerState {
433                         get {
434                                 ThrowIfDisposedAndClosed ();
435
436                                 return (LingerOption) GetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Linger);
437                         }
438                         set {
439                                 ThrowIfDisposedAndClosed ();
440                                 SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Linger, value);
441                         }
442                 }
443
444                 public bool MulticastLoopback {
445                         get {
446                                 ThrowIfDisposedAndClosed ();
447
448                                 /* Even though this option can be set for TCP sockets on Linux, throw
449                                  * this exception anyway to be compatible (the MSDN docs say
450                                  * "Setting this property on a Transmission Control Protocol (TCP)
451                                  * socket will have no effect." but the MS runtime throws the
452                                  * exception...) */
453                                 if (protocol_type == ProtocolType.Tcp)
454                                         throw new SocketException ((int)SocketError.ProtocolOption);
455
456                                 switch (address_family) {
457                                 case AddressFamily.InterNetwork:
458                                         return ((int) GetSocketOption (SocketOptionLevel.IP, SocketOptionName.MulticastLoopback)) != 0;
459                                 case AddressFamily.InterNetworkV6:
460                                         return ((int) GetSocketOption (SocketOptionLevel.IPv6, SocketOptionName.MulticastLoopback)) != 0;
461                                 default:
462                                         throw new NotSupportedException ("This property is only valid for InterNetwork and InterNetworkV6 sockets");
463                                 }
464                         }
465                         set {
466                                 ThrowIfDisposedAndClosed ();
467
468                                 /* Even though this option can be set for TCP sockets on Linux, throw
469                                  * this exception anyway to be compatible (the MSDN docs say
470                                  * "Setting this property on a Transmission Control Protocol (TCP)
471                                  * socket will have no effect." but the MS runtime throws the
472                                  * exception...) */
473                                 if (protocol_type == ProtocolType.Tcp)
474                                         throw new SocketException ((int)SocketError.ProtocolOption);
475
476                                 switch (address_family) {
477                                 case AddressFamily.InterNetwork:
478                                         SetSocketOption (SocketOptionLevel.IP, SocketOptionName.MulticastLoopback, value ? 1 : 0);
479                                         break;
480                                 case AddressFamily.InterNetworkV6:
481                                         SetSocketOption (SocketOptionLevel.IPv6, SocketOptionName.MulticastLoopback, value ? 1 : 0);
482                                         break;
483                                 default:
484                                         throw new NotSupportedException ("This property is only valid for InterNetwork and InterNetworkV6 sockets");
485                                 }
486                         }
487                 }
488
489                 public bool DualMode {
490                         get {
491                                 if (AddressFamily != AddressFamily.InterNetworkV6) 
492                                         throw new NotSupportedException("This protocol version is not supported");
493
494                                 return ((int)GetSocketOption(SocketOptionLevel.IPv6, SocketOptionName.IPv6Only) == 0);
495                         }
496                         set {
497                                 if (AddressFamily != AddressFamily.InterNetworkV6) 
498                                         throw new NotSupportedException("This protocol version is not supported");
499
500                                 SetSocketOption(SocketOptionLevel.IPv6, SocketOptionName.IPv6Only, value ? 0 : 1);
501                         }
502                 }
503
504                 private bool IsDualMode {
505                         get {
506                                 return AddressFamily == AddressFamily.InterNetworkV6 && DualMode;
507                         }
508                 }
509
510                 [MonoTODO ("This doesn't do anything on Mono yet")]
511                 public bool UseOnlyOverlappedIO {
512                         get { return use_overlapped_io; }
513                         set { use_overlapped_io = value; }
514                 }
515
516                 public IntPtr Handle {
517                         get { return safe_handle.DangerousGetHandle (); }
518                 }
519
520                 // Wish:  support non-IP endpoints.
521                 public EndPoint LocalEndPoint {
522                         get {
523                                 ThrowIfDisposedAndClosed ();
524
525                                 /* If the seed EndPoint is null, Connect, Bind, etc has not yet
526                                  * been called. MS returns null in this case. */
527                                 if (seed_endpoint == null)
528                                         return null;
529
530                                 int error;
531                                 SocketAddress sa = LocalEndPoint_internal (safe_handle, (int) address_family, out error);
532
533                                 if (error != 0)
534                                         throw new SocketException (error);
535
536                                 return seed_endpoint.Create (sa);
537                         }
538                 }
539
540                 static SocketAddress LocalEndPoint_internal (SafeSocketHandle safeHandle, int family, out int error)
541                 {
542                         bool release = false;
543                         try {
544                                 safeHandle.DangerousAddRef (ref release);
545                                 return LocalEndPoint_internal (safeHandle.DangerousGetHandle (), family, out error);
546                         } finally {
547                                 if (release)
548                                         safeHandle.DangerousRelease ();
549                         }
550                 }
551
552                 /* Returns the local endpoint details in addr and port */
553                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
554                 extern static SocketAddress LocalEndPoint_internal (IntPtr socket, int family, out int error);
555
556                 public SocketType SocketType {
557                         get { return socket_type; }
558                 }
559
560                 public int SendTimeout {
561                         get {
562                                 ThrowIfDisposedAndClosed ();
563
564                                 return (int) GetSocketOption (SocketOptionLevel.Socket, SocketOptionName.SendTimeout);
565                         }
566                         set {
567                                 ThrowIfDisposedAndClosed ();
568
569                                 if (value < -1)
570                                         throw new ArgumentOutOfRangeException ("value", "The value specified for a set operation is less than -1");
571
572                                 /* According to the MSDN docs we should adjust values between 1 and
573                                  * 499 to 500, but the MS runtime doesn't do this. */
574                                 if (value == -1)
575                                         value = 0;
576
577                                 SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendTimeout, value);
578                         }
579                 }
580
581                 public int ReceiveTimeout {
582                         get {
583                                 ThrowIfDisposedAndClosed ();
584
585                                 return (int) GetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout);
586                         }
587                         set {
588                                 ThrowIfDisposedAndClosed ();
589
590                                 if (value < -1)
591                                         throw new ArgumentOutOfRangeException ("value", "The value specified for a set operation is less than -1");
592
593                                 if (value == -1)
594                                         value = 0;
595
596                                 SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, value);
597                         }
598                 }
599
600                 public AddressFamily AddressFamily {
601                         get { return address_family; }
602                 }
603
604                 public bool Blocking {
605                         get { return is_blocking; }
606                         set {
607                                 ThrowIfDisposedAndClosed ();
608
609                                 int error;
610                                 Blocking_internal (safe_handle, value, out error);
611
612                                 if (error != 0)
613                                         throw new SocketException (error);
614
615                                 is_blocking = value;
616                         }
617                 }
618
619                 static void Blocking_internal (SafeSocketHandle safeHandle, bool block, out int error)
620                 {
621                         bool release = false;
622                         try {
623                                 safeHandle.DangerousAddRef (ref release);
624                                 Blocking_internal (safeHandle.DangerousGetHandle (), block, out error);
625                         } finally {
626                                 if (release)
627                                         safeHandle.DangerousRelease ();
628                         }
629                 }
630
631                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
632                 internal extern static void Blocking_internal(IntPtr socket, bool block, out int error);
633
634                 public bool Connected {
635                         get { return is_connected; }
636                         internal set { is_connected = value; }
637                 }
638
639                 public ProtocolType ProtocolType {
640                         get { return protocol_type; }
641                 }
642
643                 public bool NoDelay {
644                         get {
645                                 ThrowIfDisposedAndClosed ();
646                                 ThrowIfUdp ();
647
648                                 return ((int) GetSocketOption (SocketOptionLevel.Tcp, SocketOptionName.NoDelay)) != 0;
649                         }
650
651                         set {
652                                 ThrowIfDisposedAndClosed ();
653                                 ThrowIfUdp ();
654
655                                 SetSocketOption (SocketOptionLevel.Tcp, SocketOptionName.NoDelay, value ? 1 : 0);
656                         }
657                 }
658
659                 public int ReceiveBufferSize {
660                         get {
661                                 ThrowIfDisposedAndClosed ();
662
663                                 return (int) GetSocketOption (SocketOptionLevel.Socket, SocketOptionName.ReceiveBuffer);
664                         }
665                         set {
666                                 ThrowIfDisposedAndClosed ();
667
668                                 if (value < 0)
669                                         throw new ArgumentOutOfRangeException ("value", "The value specified for a set operation is less than zero");
670
671                                 SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.ReceiveBuffer, value);
672                         }
673                 }
674
675                 public int SendBufferSize {
676                         get {
677                                 ThrowIfDisposedAndClosed ();
678
679                                 return (int) GetSocketOption (SocketOptionLevel.Socket, SocketOptionName.SendBuffer);
680                         }
681                         set {
682                                 ThrowIfDisposedAndClosed ();
683
684                                 if (value < 0)
685                                         throw new ArgumentOutOfRangeException ("value", "The value specified for a set operation is less than zero");
686
687                                 SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.SendBuffer, value);
688                         }
689                 }
690
691                 public short Ttl {
692                         get {
693                                 ThrowIfDisposedAndClosed ();
694
695                                 switch (address_family) {
696                                 case AddressFamily.InterNetwork:
697                                         return (short) (int) GetSocketOption (SocketOptionLevel.IP, SocketOptionName.IpTimeToLive);
698                                 case AddressFamily.InterNetworkV6:
699                                         return (short) (int) GetSocketOption (SocketOptionLevel.IPv6, SocketOptionName.HopLimit);
700                                 default:
701                                         throw new NotSupportedException ("This property is only valid for InterNetwork and InterNetworkV6 sockets");
702                                 }
703                         }
704                         set {
705                                 ThrowIfDisposedAndClosed ();
706
707                                 if (value < 0)
708                                         throw new ArgumentOutOfRangeException ("value", "The value specified for a set operation is less than zero");
709
710                                 switch (address_family) {
711                                 case AddressFamily.InterNetwork:
712                                         SetSocketOption (SocketOptionLevel.IP, SocketOptionName.IpTimeToLive, value);
713                                         break;
714                                 case AddressFamily.InterNetworkV6:
715                                         SetSocketOption (SocketOptionLevel.IPv6, SocketOptionName.HopLimit, value);
716                                         break;
717                                 default:
718                                         throw new NotSupportedException ("This property is only valid for InterNetwork and InterNetworkV6 sockets");
719                                 }
720                         }
721                 }
722
723                 public EndPoint RemoteEndPoint {
724                         get {
725                                 ThrowIfDisposedAndClosed ();
726
727                                 /* If the seed EndPoint is null, Connect, Bind, etc has
728                                  * not yet been called. MS returns null in this case. */
729                                 if (!is_connected || seed_endpoint == null)
730                                         return null;
731
732                                 int error;
733                                 SocketAddress sa = RemoteEndPoint_internal (safe_handle, (int) address_family, out error);
734
735                                 if (error != 0)
736                                         throw new SocketException (error);
737
738                                 return seed_endpoint.Create (sa);
739                         }
740                 }
741
742                 static SocketAddress RemoteEndPoint_internal (SafeSocketHandle safeHandle, int family, out int error)
743                 {
744                         bool release = false;
745                         try {
746                                 safeHandle.DangerousAddRef (ref release);
747                                 return RemoteEndPoint_internal (safeHandle.DangerousGetHandle (), family, out error);
748                         } finally {
749                                 if (release)
750                                         safeHandle.DangerousRelease ();
751                         }
752                 }
753
754                 /* Returns the remote endpoint details in addr and port */
755                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
756                 extern static SocketAddress RemoteEndPoint_internal (IntPtr socket, int family, out int error);
757
758 #endregion
759
760 #region Select
761
762                 public static void Select (IList checkRead, IList checkWrite, IList checkError, int microSeconds)
763                 {
764                         var list = new List<Socket> ();
765                         AddSockets (list, checkRead, "checkRead");
766                         AddSockets (list, checkWrite, "checkWrite");
767                         AddSockets (list, checkError, "checkError");
768
769                         if (list.Count == 3)
770                                 throw new ArgumentNullException ("checkRead, checkWrite, checkError", "All the lists are null or empty.");
771
772                         /* The 'sockets' array contains:
773                          *  - READ socket 0-n, null,
774                          *  - WRITE socket 0-n, null,
775                          *  - ERROR socket 0-n, null */
776                         Socket [] sockets = list.ToArray ();
777
778                         int error;
779                         Select_internal (ref sockets, microSeconds, out error);
780
781                         if (error != 0)
782                                 throw new SocketException (error);
783
784                         if (sockets == null) {
785                                 if (checkRead != null)
786                                         checkRead.Clear ();
787                                 if (checkWrite != null)
788                                         checkWrite.Clear ();
789                                 if (checkError != null)
790                                         checkError.Clear ();
791                                 return;
792                         }
793
794                         int mode = 0;
795                         int count = sockets.Length;
796                         IList currentList = checkRead;
797                         int currentIdx = 0;
798                         for (int i = 0; i < count; i++) {
799                                 Socket sock = sockets [i];
800                                 if (sock == null) { // separator
801                                         if (currentList != null) {
802                                                 // Remove non-signaled sockets after the current one
803                                                 int to_remove = currentList.Count - currentIdx;
804                                                 for (int k = 0; k < to_remove; k++)
805                                                         currentList.RemoveAt (currentIdx);
806                                         }
807                                         currentList = (mode == 0) ? checkWrite : checkError;
808                                         currentIdx = 0;
809                                         mode++;
810                                         continue;
811                                 }
812
813                                 if (mode == 1 && currentList == checkWrite && !sock.is_connected) {
814                                         if ((int) sock.GetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Error) == 0)
815                                                 sock.is_connected = true;
816                                 }
817
818                                 /* Remove non-signaled sockets before the current one */
819                                 while (((Socket) currentList [currentIdx]) != sock)
820                                         currentList.RemoveAt (currentIdx);
821
822                                 currentIdx++;
823                         }
824                 }
825
826                 static void AddSockets (List<Socket> sockets, IList list, string name)
827                 {
828                         if (list != null) {
829                                 foreach (Socket sock in list) {
830                                         if (sock == null) // MS throws a NullRef
831                                                 throw new ArgumentNullException ("name", "Contains a null element");
832                                         sockets.Add (sock);
833                                 }
834                         }
835
836                         sockets.Add (null);
837                 }
838
839                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
840                 extern static void Select_internal (ref Socket [] sockets, int microSeconds, out int error);
841
842 #endregion
843
844 #region Poll
845
846                 public bool Poll (int time_us, SelectMode mode)
847                 {
848                         ThrowIfDisposedAndClosed ();
849
850                         if (mode != SelectMode.SelectRead && mode != SelectMode.SelectWrite && mode != SelectMode.SelectError)
851                                 throw new NotSupportedException ("'mode' parameter is not valid.");
852
853                         int error;
854                         bool result = Poll_internal (safe_handle, mode, time_us, out error);
855
856                         if (error != 0)
857                                 throw new SocketException (error);
858
859                         if (mode == SelectMode.SelectWrite && result && !is_connected) {
860                                 /* Update the is_connected state; for non-blocking Connect()
861                                  * this is when we can find out that the connect succeeded. */
862                                 if ((int) GetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Error) == 0)
863                                         is_connected = true;
864                         }
865
866                         return result;
867                 }
868
869                 static bool Poll_internal (SafeSocketHandle safeHandle, SelectMode mode, int timeout, out int error)
870                 {
871                         bool release = false;
872                         try {
873                                 safeHandle.DangerousAddRef (ref release);
874                                 return Poll_internal (safeHandle.DangerousGetHandle (), mode, timeout, out error);
875                         } finally {
876                                 if (release)
877                                         safeHandle.DangerousRelease ();
878                         }
879                 }
880
881                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
882                 extern static bool Poll_internal (IntPtr socket, SelectMode mode, int timeout, out int error);
883
884 #endregion
885
886 #region Accept
887
888                 public Socket Accept()
889                 {
890                         ThrowIfDisposedAndClosed ();
891
892                         int error = 0;
893                         SafeSocketHandle safe_handle = Accept_internal (this.safe_handle, out error, is_blocking);
894
895                         if (error != 0) {
896                                 if (is_closed)
897                                         error = SOCKET_CLOSED_CODE;
898                                 throw new SocketException(error);
899                         }
900
901                         Socket accepted = new Socket (this.AddressFamily, this.SocketType, this.ProtocolType, safe_handle) {
902                                 seed_endpoint = this.seed_endpoint,
903                                 Blocking = this.Blocking,
904                         };
905
906                         return accepted;
907                 }
908
909                 internal void Accept (Socket acceptSocket)
910                 {
911                         ThrowIfDisposedAndClosed ();
912
913                         int error = 0;
914                         SafeSocketHandle safe_handle = Accept_internal (this.safe_handle, out error, is_blocking);
915
916                         if (error != 0) {
917                                 if (is_closed)
918                                         error = SOCKET_CLOSED_CODE;
919                                 throw new SocketException (error);
920                         }
921
922                         acceptSocket.address_family = this.AddressFamily;
923                         acceptSocket.socket_type = this.SocketType;
924                         acceptSocket.protocol_type = this.ProtocolType;
925                         acceptSocket.safe_handle = safe_handle;
926                         acceptSocket.is_connected = true;
927                         acceptSocket.seed_endpoint = this.seed_endpoint;
928                         acceptSocket.Blocking = this.Blocking;
929
930                         // FIXME: figure out what if anything else needs to be reset
931                 }
932
933                 public bool AcceptAsync (SocketAsyncEventArgs e)
934                 {
935                         // NO check is made whether e != null in MS.NET (NRE is thrown in such case)
936
937                         ThrowIfDisposedAndClosed ();
938
939                         if (!is_bound)
940                                 throw new InvalidOperationException ("You must call the Bind method before performing this operation.");
941                         if (!is_listening)
942                                 throw new InvalidOperationException ("You must call the Listen method before performing this operation.");
943                         if (e.BufferList != null)
944                                 throw new ArgumentException ("Multiple buffers cannot be used with this method.");
945                         if (e.Count < 0)
946                                 throw new ArgumentOutOfRangeException ("e.Count");
947
948                         Socket acceptSocket = e.AcceptSocket;
949                         if (acceptSocket != null) {
950                                 if (acceptSocket.is_bound || acceptSocket.is_connected)
951                                         throw new InvalidOperationException ("AcceptSocket: The socket must not be bound or connected.");
952                         }
953
954                         e.curSocket = this;
955                         e.Worker.Init (this, e, SocketOperation.Accept);
956
957                         SocketAsyncResult sockares = e.Worker.result;
958
959                         QueueIOSelectorJob (readQ, sockares.handle, new IOSelectorJob (IOOperation.Read, s => ((SocketAsyncResult) s).Worker.Accept (), sockares));
960
961                         return true;
962                 }
963
964                 public IAsyncResult BeginAccept(AsyncCallback callback, object state)
965                 {
966                         ThrowIfDisposedAndClosed ();
967
968                         if (!is_bound || !is_listening)
969                                 throw new InvalidOperationException ();
970
971                         SocketAsyncResult sockares = new SocketAsyncResult (this, callback, state, SocketOperation.Accept);
972
973                         QueueIOSelectorJob (readQ, sockares.handle, new IOSelectorJob (IOOperation.Read, s => ((SocketAsyncResult) s).Worker.Accept (), sockares));
974
975                         return sockares;
976                 }
977
978                 public IAsyncResult BeginAccept (int receiveSize, AsyncCallback callback, object state)
979                 {
980                         ThrowIfDisposedAndClosed ();
981
982                         if (receiveSize < 0)
983                                 throw new ArgumentOutOfRangeException ("receiveSize", "receiveSize is less than zero");
984
985                         SocketAsyncResult sockares = new SocketAsyncResult (this, callback, state, SocketOperation.AcceptReceive) {
986                                 Buffer = new byte [receiveSize],
987                                 Offset = 0,
988                                 Size = receiveSize,
989                                 SockFlags = SocketFlags.None,
990                         };
991
992                         QueueIOSelectorJob (readQ, sockares.handle, new IOSelectorJob (IOOperation.Read, s => ((SocketAsyncResult) s).Worker.AcceptReceive (), sockares));
993
994                         return sockares;
995                 }
996
997                 public IAsyncResult BeginAccept (Socket acceptSocket, int receiveSize, AsyncCallback callback, object state)
998                 {
999                         ThrowIfDisposedAndClosed ();
1000
1001                         if (receiveSize < 0)
1002                                 throw new ArgumentOutOfRangeException ("receiveSize", "receiveSize is less than zero");
1003
1004                         if (acceptSocket != null) {
1005                                 ThrowIfDisposedAndClosed (acceptSocket);
1006
1007                                 if (acceptSocket.IsBound)
1008                                         throw new InvalidOperationException ();
1009
1010                                 /* For some reason the MS runtime
1011                                  * barfs if the new socket is not TCP,
1012                                  * even though it's just about to blow
1013                                  * away all those parameters
1014                                  */
1015                                 if (acceptSocket.ProtocolType != ProtocolType.Tcp)
1016                                         throw new SocketException ((int)SocketError.InvalidArgument);
1017                         }
1018                         
1019                         SocketAsyncResult sockares = new SocketAsyncResult (this, callback, state, SocketOperation.AcceptReceive) {
1020                                 Buffer = new byte [receiveSize],
1021                                 Offset = 0,
1022                                 Size = receiveSize,
1023                                 SockFlags = SocketFlags.None,
1024                                 AcceptSocket = acceptSocket,
1025                         };
1026
1027                         QueueIOSelectorJob (readQ, sockares.handle, new IOSelectorJob (IOOperation.Read, s => ((SocketAsyncResult) s).Worker.AcceptReceive (), sockares));
1028
1029                         return sockares;
1030                 }
1031
1032                 public Socket EndAccept (IAsyncResult result)
1033                 {
1034                         int bytes;
1035                         byte[] buffer;
1036                         return EndAccept (out buffer, out bytes, result);
1037                 }
1038
1039                 public Socket EndAccept (out byte[] buffer, IAsyncResult asyncResult)
1040                 {
1041                         int bytes;
1042                         return EndAccept (out buffer, out bytes, asyncResult);
1043                 }
1044
1045                 public Socket EndAccept (out byte[] buffer, out int bytesTransferred, IAsyncResult asyncResult)
1046                 {
1047                         ThrowIfDisposedAndClosed ();
1048
1049                         SocketAsyncResult sockares = ValidateEndIAsyncResult (asyncResult, "EndAccept", "asyncResult");
1050
1051                         if (!sockares.IsCompleted)
1052                                 sockares.AsyncWaitHandle.WaitOne ();
1053
1054                         sockares.CheckIfThrowDelayedException ();
1055
1056                         buffer = sockares.Buffer;
1057                         bytesTransferred = sockares.Total;
1058
1059                         return sockares.Socket;
1060                 }
1061
1062                 static SafeSocketHandle Accept_internal (SafeSocketHandle safeHandle, out int error, bool blocking)
1063                 {
1064                         try {
1065                                 safeHandle.RegisterForBlockingSyscall ();
1066                                 var ret = Accept_internal (safeHandle.DangerousGetHandle (), out error, blocking);
1067                                 return new SafeSocketHandle (ret, true);
1068                         } finally {
1069                                 safeHandle.UnRegisterForBlockingSyscall ();
1070                         }
1071                 }
1072
1073                 /* Creates a new system socket, returning the handle */
1074                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1075                 extern static IntPtr Accept_internal (IntPtr sock, out int error, bool blocking);
1076
1077 #endregion
1078
1079 #region Bind
1080
1081                 public void Bind (EndPoint local_end)
1082                 {
1083                         ThrowIfDisposedAndClosed ();
1084
1085                         if (local_end == null)
1086                                 throw new ArgumentNullException("local_end");
1087
1088                         int error;
1089                         Bind_internal (safe_handle, local_end.Serialize(), out error);
1090
1091                         if (error != 0)
1092                                 throw new SocketException (error);
1093                         if (error == 0)
1094                                 is_bound = true;
1095
1096                         seed_endpoint = local_end;
1097                 }
1098
1099                 private static void Bind_internal (SafeSocketHandle safeHandle, SocketAddress sa, out int error)
1100                 {
1101                         bool release = false;
1102                         try {
1103                                 safeHandle.DangerousAddRef (ref release);
1104                                 Bind_internal (safeHandle.DangerousGetHandle (), sa, out error);
1105                         } finally {
1106                                 if (release)
1107                                         safeHandle.DangerousRelease ();
1108                         }
1109                 }
1110
1111                 // Creates a new system socket, returning the handle
1112                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1113                 private extern static void Bind_internal(IntPtr sock, SocketAddress sa, out int error);
1114
1115 #endregion
1116
1117 #region Listen
1118
1119                 public void Listen (int backlog)
1120                 {
1121                         ThrowIfDisposedAndClosed ();
1122
1123                         if (!is_bound)
1124                                 throw new SocketException ((int) SocketError.InvalidArgument);
1125
1126                         int error;
1127                         Listen_internal(safe_handle, backlog, out error);
1128
1129                         if (error != 0)
1130                                 throw new SocketException (error);
1131
1132                         is_listening = true;
1133                 }
1134
1135                 static void Listen_internal (SafeSocketHandle safeHandle, int backlog, out int error)
1136                 {
1137                         bool release = false;
1138                         try {
1139                                 safeHandle.DangerousAddRef (ref release);
1140                                 Listen_internal (safeHandle.DangerousGetHandle (), backlog, out error);
1141                         } finally {
1142                                 if (release)
1143                                         safeHandle.DangerousRelease ();
1144                         }
1145                 }
1146
1147                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1148                 extern static void Listen_internal (IntPtr sock, int backlog, out int error);
1149
1150 #endregion
1151
1152 #region Connect
1153
1154                 public void Connect (IPAddress address, int port)
1155                 {
1156                         Connect (new IPEndPoint (address, port));
1157                 }
1158
1159                 public void Connect (string host, int port)
1160                 {
1161                         Connect (Dns.GetHostAddresses (host), port);
1162                 }
1163
1164                 public void Connect (IPAddress[] addresses, int port)
1165                 {
1166                         ThrowIfDisposedAndClosed ();
1167
1168                         if (addresses == null)
1169                                 throw new ArgumentNullException ("addresses");
1170                         if (this.AddressFamily != AddressFamily.InterNetwork && this.AddressFamily != AddressFamily.InterNetworkV6)
1171                                 throw new NotSupportedException ("This method is only valid for addresses in the InterNetwork or InterNetworkV6 families");
1172                         if (is_listening)
1173                                 throw new InvalidOperationException ();
1174
1175                         // FIXME: do non-blocking sockets Poll here?
1176                         int error = 0;
1177                         foreach (IPAddress address in addresses) {
1178                                 IPEndPoint iep = new IPEndPoint (address, port);
1179
1180                                 Connect_internal (safe_handle, iep.Serialize (), out error);
1181                                 if (error == 0) {
1182                                         is_connected = true;
1183                                         is_bound = true;
1184                                         seed_endpoint = iep;
1185                                         return;
1186                                 }
1187                                 if (error != (int)SocketError.InProgress && error != (int)SocketError.WouldBlock)
1188                                         continue;
1189
1190                                 if (!is_blocking) {
1191                                         Poll (-1, SelectMode.SelectWrite);
1192                                         error = (int)GetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Error);
1193                                         if (error == 0) {
1194                                                 is_connected = true;
1195                                                 is_bound = true;
1196                                                 seed_endpoint = iep;
1197                                                 return;
1198                                         }
1199                                 }
1200                         }
1201
1202                         if (error != 0)
1203                                 throw new SocketException (error);
1204                 }
1205
1206
1207                 public void Connect (EndPoint remoteEP)
1208                 {
1209                         ThrowIfDisposedAndClosed ();
1210
1211                         if (remoteEP == null)
1212                                 throw new ArgumentNullException ("remoteEP");
1213
1214                         IPEndPoint ep = remoteEP as IPEndPoint;
1215                         /* Dgram uses Any to 'disconnect' */
1216                         if (ep != null && socket_type != SocketType.Dgram) {
1217                                 if (ep.Address.Equals (IPAddress.Any) || ep.Address.Equals (IPAddress.IPv6Any))
1218                                         throw new SocketException ((int) SocketError.AddressNotAvailable);
1219                         }
1220
1221                         if (is_listening)
1222                                 throw new InvalidOperationException ();
1223
1224                         SocketAddress serial = remoteEP.Serialize ();
1225
1226                         int error = 0;
1227                         Connect_internal (safe_handle, serial, out error);
1228
1229                         if (error == 0 || error == 10035)
1230                                 seed_endpoint = remoteEP; // Keep the ep around for non-blocking sockets
1231
1232                         if (error != 0) {
1233                                 if (is_closed)
1234                                         error = SOCKET_CLOSED_CODE;
1235                                 throw new SocketException (error);
1236                         }
1237
1238                         is_connected = !(socket_type == SocketType.Dgram && ep != null && (ep.Address.Equals (IPAddress.Any) || ep.Address.Equals (IPAddress.IPv6Any)));
1239                         is_bound = true;
1240                 }
1241
1242                 public bool ConnectAsync (SocketAsyncEventArgs e)
1243                 {
1244                         // NO check is made whether e != null in MS.NET (NRE is thrown in such case)
1245
1246                         ThrowIfDisposedAndClosed ();
1247
1248                         if (is_listening)
1249                                 throw new InvalidOperationException ("You may not perform this operation after calling the Listen method.");
1250                         if (e.RemoteEndPoint == null)
1251                                 throw new ArgumentNullException ("remoteEP");
1252
1253                         e.curSocket = this;
1254                         e.Worker.Init (this, e, SocketOperation.Connect);
1255
1256                         SocketAsyncResult result = e.Worker.result;
1257
1258                         try {
1259                                 IPAddress [] addresses;
1260                                 IAsyncResult ares;
1261
1262                                 if (!GetCheckedIPs (e, out addresses)) {
1263                                         result.EndPoint = e.RemoteEndPoint;
1264                                         ares = BeginConnect (e.RemoteEndPoint, SocketAsyncEventArgs.Dispatcher, e);
1265                                 } else {
1266                                         DnsEndPoint dep = (e.RemoteEndPoint as DnsEndPoint);
1267                                         result.Addresses = addresses;
1268                                         result.Port = dep.Port;
1269                                         ares = BeginConnect (addresses, dep.Port, SocketAsyncEventArgs.Dispatcher, e);
1270                                 }
1271
1272                                 if (ares.IsCompleted && ares.CompletedSynchronously) {
1273                                         ((SocketAsyncResult) ares).CheckIfThrowDelayedException ();
1274                                         return false;
1275                                 }
1276                         } catch (Exception exc) {
1277                                 result.Complete (exc, true);
1278                                 return false;
1279                         }
1280
1281                         return true;
1282                 }
1283
1284                 public IAsyncResult BeginConnect (IPAddress address, int port, AsyncCallback callback, object state)
1285                 {
1286                         ThrowIfDisposedAndClosed ();
1287
1288                         if (address == null)
1289                                 throw new ArgumentNullException ("address");
1290                         if (address.ToString ().Length == 0)
1291                                 throw new ArgumentException ("The length of the IP address is zero");
1292                         if (port <= 0 || port > 65535)
1293                                 throw new ArgumentOutOfRangeException ("port", "Must be > 0 and < 65536");
1294                         if (is_listening)
1295                                 throw new InvalidOperationException ();
1296
1297                         return BeginConnect (new IPEndPoint (address, port), callback, state);
1298                 }
1299
1300                 public IAsyncResult BeginConnect (string host, int port, AsyncCallback callback, object state)
1301                 {
1302                         ThrowIfDisposedAndClosed ();
1303
1304                         if (host == null)
1305                                 throw new ArgumentNullException ("host");
1306                         if (address_family != AddressFamily.InterNetwork && address_family != AddressFamily.InterNetworkV6)
1307                                 throw new NotSupportedException ("This method is valid only for sockets in the InterNetwork and InterNetworkV6 families");
1308                         if (port <= 0 || port > 65535)
1309                                 throw new ArgumentOutOfRangeException ("port", "Must be > 0 and < 65536");
1310                         if (is_listening)
1311                                 throw new InvalidOperationException ();
1312
1313                         return BeginConnect (Dns.GetHostAddresses (host), port, callback, state);
1314                 }
1315
1316                 public IAsyncResult BeginConnect (EndPoint end_point, AsyncCallback callback, object state)
1317                 {
1318                         ThrowIfDisposedAndClosed ();
1319
1320                         if (end_point == null)
1321                                 throw new ArgumentNullException ("end_point");
1322
1323                         SocketAsyncResult sockares = new SocketAsyncResult (this, callback, state, SocketOperation.Connect) {
1324                                 EndPoint = end_point,
1325                         };
1326
1327                         // Bug #75154: Connect() should not succeed for .Any addresses.
1328                         if (end_point is IPEndPoint) {
1329                                 IPEndPoint ep = (IPEndPoint) end_point;
1330                                 if (ep.Address.Equals (IPAddress.Any) || ep.Address.Equals (IPAddress.IPv6Any)) {
1331                                         sockares.Complete (new SocketException ((int) SocketError.AddressNotAvailable), true);
1332                                         return sockares;
1333                                 }
1334                         }
1335
1336                         int error = 0;
1337
1338                         if (connect_in_progress) {
1339                                 // This could happen when multiple IPs are used
1340                                 // Calling connect() again will reset the connection attempt and cause
1341                                 // an error. Better to just close the socket and move on.
1342                                 connect_in_progress = false;
1343                                 safe_handle.Dispose ();
1344                                 var handle = Socket_internal (address_family, socket_type, protocol_type, out error);
1345                                 safe_handle = new SafeSocketHandle (handle, true);
1346                                 if (error != 0)
1347                                         throw new SocketException (error);
1348                         }
1349
1350                         bool blk = is_blocking;
1351                         if (blk)
1352                                 Blocking = false;
1353                         Connect_internal (safe_handle, end_point.Serialize (), out error);
1354                         if (blk)
1355                                 Blocking = true;
1356
1357                         if (error == 0) {
1358                                 // succeeded synch
1359                                 is_connected = true;
1360                                 is_bound = true;
1361                                 sockares.Complete (true);
1362                                 return sockares;
1363                         }
1364
1365                         if (error != (int) SocketError.InProgress && error != (int) SocketError.WouldBlock) {
1366                                 // error synch
1367                                 is_connected = false;
1368                                 is_bound = false;
1369                                 sockares.Complete (new SocketException (error), true);
1370                                 return sockares;
1371                         }
1372
1373                         // continue asynch
1374                         is_connected = false;
1375                         is_bound = false;
1376                         connect_in_progress = true;
1377
1378                         IOSelector.Add (sockares.handle, new IOSelectorJob (IOOperation.Write, s => ((SocketAsyncResult) s).Worker.Connect (), sockares));
1379
1380                         return sockares;
1381                 }
1382
1383                 public IAsyncResult BeginConnect (IPAddress[] addresses, int port, AsyncCallback callback, object state)
1384                 {
1385                         ThrowIfDisposedAndClosed ();
1386
1387                         if (addresses == null)
1388                                 throw new ArgumentNullException ("addresses");
1389                         if (addresses.Length == 0)
1390                                 throw new ArgumentException ("Empty addresses list");
1391                         if (this.AddressFamily != AddressFamily.InterNetwork && this.AddressFamily != AddressFamily.InterNetworkV6)
1392                                 throw new NotSupportedException ("This method is only valid for addresses in the InterNetwork or InterNetworkV6 families");
1393                         if (port <= 0 || port > 65535)
1394                                 throw new ArgumentOutOfRangeException ("port", "Must be > 0 and < 65536");
1395                         if (is_listening)
1396                                 throw new InvalidOperationException ();
1397
1398                         SocketAsyncResult sockares = new SocketAsyncResult (this, callback, state, SocketOperation.Connect) {
1399                                 Addresses = addresses,
1400                                 Port = port,
1401                         };
1402
1403                         is_connected = false;
1404
1405                         return BeginMConnect (sockares);
1406                 }
1407
1408                 internal IAsyncResult BeginMConnect (SocketAsyncResult sockares)
1409                 {
1410                         SocketAsyncResult ares = null;
1411                         Exception exc = null;
1412                         AsyncCallback callback;
1413
1414                         for (int i = sockares.CurrentAddress; i < sockares.Addresses.Length; i++) {
1415                                 try {
1416                                         sockares.CurrentAddress++;
1417
1418                                         ares = (SocketAsyncResult) BeginConnect (new IPEndPoint (sockares.Addresses [i], sockares.Port), null, sockares);
1419                                         if (ares.IsCompleted && ares.CompletedSynchronously) {
1420                                                 ares.CheckIfThrowDelayedException ();
1421
1422                                                 callback = ares.AsyncCallback;
1423                                                 if (callback != null)
1424                                                         ThreadPool.UnsafeQueueUserWorkItem (_ => callback (ares), null);
1425                                         }
1426
1427                                         break;
1428                                 } catch (Exception e) {
1429                                         exc = e;
1430                                         ares = null;
1431                                 }
1432                         }
1433
1434                         if (ares == null)
1435                                 throw exc;
1436
1437                         return sockares;
1438                 }
1439
1440                 public void EndConnect (IAsyncResult result)
1441                 {
1442                         ThrowIfDisposedAndClosed ();
1443
1444                         SocketAsyncResult sockares = ValidateEndIAsyncResult (result, "EndConnect", "result");
1445
1446                         if (!sockares.IsCompleted)
1447                                 sockares.AsyncWaitHandle.WaitOne();
1448
1449                         sockares.CheckIfThrowDelayedException();
1450                 }
1451
1452                 static void Connect_internal (SafeSocketHandle safeHandle, SocketAddress sa, out int error)
1453                 {
1454                         try {
1455                                 safeHandle.RegisterForBlockingSyscall ();
1456                                 Connect_internal (safeHandle.DangerousGetHandle (), sa, out error);
1457                         } finally {
1458                                 safeHandle.UnRegisterForBlockingSyscall ();
1459                         }
1460                 }
1461
1462                 /* Connects to the remote address */
1463                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1464                 extern static void Connect_internal(IntPtr sock, SocketAddress sa, out int error);
1465
1466                 /* Returns :
1467                  *  - false when it is ok to use RemoteEndPoint
1468                  *  - true when addresses must be used (and addresses could be null/empty) */
1469                 bool GetCheckedIPs (SocketAsyncEventArgs e, out IPAddress [] addresses)
1470                 {
1471                         addresses = null;
1472
1473                         // Connect to the first address that match the host name, like:
1474                         // http://blogs.msdn.com/ncl/archive/2009/07/20/new-ncl-features-in-net-4-0-beta-2.aspx
1475                         // while skipping entries that do not match the address family
1476                         DnsEndPoint dep = e.RemoteEndPoint as DnsEndPoint;
1477                         if (dep != null) {
1478                                 addresses = Dns.GetHostAddresses (dep.Host);
1479                                 return true;
1480                         } else {
1481                                 e.ConnectByNameError = null;
1482                                 return false;
1483                         }
1484                 }
1485
1486 #endregion
1487
1488 #region Disconnect
1489
1490                 /* According to the docs, the MS runtime will throw PlatformNotSupportedException
1491                  * if the platform is newer than w2k.  We should be able to cope... */
1492                 public void Disconnect (bool reuseSocket)
1493                 {
1494                         ThrowIfDisposedAndClosed ();
1495
1496                         int error = 0;
1497                         Disconnect_internal (safe_handle, reuseSocket, out error);
1498
1499                         if (error != 0) {
1500                                 if (error == 50) {
1501                                         /* ERROR_NOT_SUPPORTED */
1502                                         throw new PlatformNotSupportedException ();
1503                                 } else {
1504                                         throw new SocketException (error);
1505                                 }
1506                         }
1507
1508                         is_connected = false;
1509                         if (reuseSocket) {
1510                                 /* Do managed housekeeping here... */
1511                         }
1512                 }
1513
1514                 public bool DisconnectAsync (SocketAsyncEventArgs e)
1515                 {
1516                         // NO check is made whether e != null in MS.NET (NRE is thrown in such case)
1517
1518                         ThrowIfDisposedAndClosed ();
1519
1520                         e.curSocket = this;
1521                         e.Worker.Init (this, e, SocketOperation.Disconnect);
1522
1523                         SocketAsyncResult sockares = e.Worker.result;
1524
1525                         IOSelector.Add (sockares.handle, new IOSelectorJob (IOOperation.Write, s => ((SocketAsyncResult) s).Worker.Disconnect (), sockares));
1526
1527                         return true;
1528                 }
1529
1530
1531                 public IAsyncResult BeginDisconnect (bool reuseSocket, AsyncCallback callback, object state)
1532                 {
1533                         ThrowIfDisposedAndClosed ();
1534
1535                         SocketAsyncResult sockares = new SocketAsyncResult (this, callback, state, SocketOperation.Disconnect) {
1536                                 ReuseSocket = reuseSocket,
1537                         };
1538
1539                         IOSelector.Add (sockares.handle, new IOSelectorJob (IOOperation.Write, s => ((SocketAsyncResult) s).Worker.Disconnect (), sockares));
1540
1541                         return sockares;
1542                 }
1543
1544                 public void EndDisconnect (IAsyncResult asyncResult)
1545                 {
1546                         ThrowIfDisposedAndClosed ();
1547
1548                         SocketAsyncResult sockares = ValidateEndIAsyncResult (asyncResult, "EndDisconnect", "asyncResult");
1549
1550                         if (!sockares.IsCompleted)
1551                                 sockares.AsyncWaitHandle.WaitOne ();
1552
1553                         sockares.CheckIfThrowDelayedException ();
1554                 }
1555
1556                 static void Disconnect_internal (SafeSocketHandle safeHandle, bool reuse, out int error)
1557                 {
1558                         bool release = false;
1559                         try {
1560                                 safeHandle.DangerousAddRef (ref release);
1561                                 Disconnect_internal (safeHandle.DangerousGetHandle (), reuse, out error);
1562                         } finally {
1563                                 if (release)
1564                                         safeHandle.DangerousRelease ();
1565                         }
1566                 }
1567
1568                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1569                 extern static void Disconnect_internal (IntPtr sock, bool reuse, out int error);
1570
1571 #endregion
1572
1573 #region Receive
1574
1575                 public int Receive (byte [] buffer)
1576                 {
1577                         return Receive (buffer, SocketFlags.None);
1578                 }
1579
1580                 public int Receive (byte [] buffer, SocketFlags flags)
1581                 {
1582                         ThrowIfDisposedAndClosed ();
1583                         ThrowIfBufferNull (buffer);
1584                         ThrowIfBufferOutOfRange (buffer, 0, buffer.Length);
1585
1586                         SocketError error;
1587                         int ret = Receive_nochecks (buffer, 0, buffer.Length, flags, out error);
1588
1589                         if (error != SocketError.Success) {
1590                                 if (error == SocketError.WouldBlock && is_blocking) // This might happen when ReceiveTimeout is set
1591                                         throw new SocketException ((int) error, TIMEOUT_EXCEPTION_MSG);
1592                                 throw new SocketException ((int) error);
1593                         }
1594
1595                         return ret;
1596                 }
1597
1598                 public int Receive (byte [] buffer, int size, SocketFlags flags)
1599                 {
1600                         ThrowIfDisposedAndClosed ();
1601                         ThrowIfBufferNull (buffer);
1602                         ThrowIfBufferOutOfRange (buffer, 0, size);
1603
1604                         SocketError error;
1605                         int ret = Receive_nochecks (buffer, 0, size, flags, out error);
1606
1607                         if (error != SocketError.Success) {
1608                                 if (error == SocketError.WouldBlock && is_blocking) // This might happen when ReceiveTimeout is set
1609                                         throw new SocketException ((int) error, TIMEOUT_EXCEPTION_MSG);
1610                                 throw new SocketException ((int) error);
1611                         }
1612
1613                         return ret;
1614                 }
1615
1616                 public int Receive (byte [] buffer, int offset, int size, SocketFlags flags)
1617                 {
1618                         ThrowIfDisposedAndClosed ();
1619                         ThrowIfBufferNull (buffer);
1620                         ThrowIfBufferOutOfRange (buffer, offset, size);
1621
1622                         SocketError error;
1623                         int ret = Receive_nochecks (buffer, offset, size, flags, out error);
1624
1625                         if (error != SocketError.Success) {
1626                                 if (error == SocketError.WouldBlock && is_blocking) // This might happen when ReceiveTimeout is set
1627                                         throw new SocketException ((int) error, TIMEOUT_EXCEPTION_MSG);
1628                                 throw new SocketException ((int) error);
1629                         }
1630
1631                         return ret;
1632                 }
1633
1634                 public int Receive (byte [] buffer, int offset, int size, SocketFlags flags, out SocketError error)
1635                 {
1636                         ThrowIfDisposedAndClosed ();
1637                         ThrowIfBufferNull (buffer);
1638                         ThrowIfBufferOutOfRange (buffer, offset, size);
1639
1640                         return Receive_nochecks (buffer, offset, size, flags, out error);
1641                 }
1642
1643                 public int Receive (IList<ArraySegment<byte>> buffers)
1644                 {
1645                         SocketError error;
1646                         int ret = Receive (buffers, SocketFlags.None, out error);
1647
1648                         if (error != SocketError.Success)
1649                                 throw new SocketException ((int) error);
1650
1651                         return ret;
1652                 }
1653
1654                 [CLSCompliant (false)]
1655                 public int Receive (IList<ArraySegment<byte>> buffers, SocketFlags socketFlags)
1656                 {
1657                         SocketError error;
1658                         int ret = Receive (buffers, socketFlags, out error);
1659
1660                         if (error != SocketError.Success)
1661                                 throw new SocketException ((int) error);
1662
1663                         return(ret);
1664                 }
1665
1666                 [CLSCompliant (false)]
1667                 public int Receive (IList<ArraySegment<byte>> buffers, SocketFlags socketFlags, out SocketError errorCode)
1668                 {
1669                         ThrowIfDisposedAndClosed ();
1670
1671                         if (buffers == null || buffers.Count == 0)
1672                                 throw new ArgumentNullException ("buffers");
1673
1674                         int numsegments = buffers.Count;
1675                         int nativeError;
1676                         int ret;
1677
1678                         /* Only example I can find of sending a byte array reference directly into an internal
1679                          * call is in System.Runtime.Remoting/System.Runtime.Remoting.Channels.Ipc.Win32/NamedPipeSocket.cs,
1680                          * so taking a lead from that... */
1681                         WSABUF[] bufarray = new WSABUF[numsegments];
1682                         GCHandle[] gch = new GCHandle[numsegments];
1683
1684                         for (int i = 0; i < numsegments; i++) {
1685                                 ArraySegment<byte> segment = buffers[i];
1686
1687                                 if (segment.Offset < 0 || segment.Count < 0 || segment.Count > segment.Array.Length - segment.Offset)
1688                                         throw new ArgumentOutOfRangeException ("segment");
1689
1690                                 gch[i] = GCHandle.Alloc (segment.Array, GCHandleType.Pinned);
1691                                 bufarray[i].len = segment.Count;
1692                                 bufarray[i].buf = Marshal.UnsafeAddrOfPinnedArrayElement (segment.Array, segment.Offset);
1693                         }
1694
1695                         try {
1696                                 ret = Receive_internal (safe_handle, bufarray, socketFlags, out nativeError);
1697                         } finally {
1698                                 for (int i = 0; i < numsegments; i++) {
1699                                         if (gch[i].IsAllocated)
1700                                                 gch[i].Free ();
1701                                 }
1702                         }
1703
1704                         errorCode = (SocketError) nativeError;
1705
1706                         return ret;
1707                 }
1708
1709                 public bool ReceiveAsync (SocketAsyncEventArgs e)
1710                 {
1711                         // NO check is made whether e != null in MS.NET (NRE is thrown in such case)
1712
1713                         ThrowIfDisposedAndClosed ();
1714
1715                         // LAME SPEC: the ArgumentException is never thrown, instead an NRE is
1716                         // thrown when e.Buffer and e.BufferList are null (works fine when one is
1717                         // set to a valid object)
1718                         if (e.Buffer == null && e.BufferList == null)
1719                                 throw new NullReferenceException ("Either e.Buffer or e.BufferList must be valid buffers.");
1720
1721                         e.curSocket = this;
1722                         e.Worker.Init (this, e, e.Buffer != null ? SocketOperation.Receive : SocketOperation.ReceiveGeneric);
1723
1724                         SocketAsyncResult sockares = e.Worker.result;
1725                         sockares.SockFlags = e.SocketFlags;
1726
1727                         if (e.Buffer != null) {
1728                                 sockares.Buffer = e.Buffer;
1729                                 sockares.Offset = e.Offset;
1730                                 sockares.Size = e.Count;
1731                         } else {
1732                                 sockares.Buffers = e.BufferList;
1733                         }
1734
1735                         QueueIOSelectorJob (readQ, sockares.handle, new IOSelectorJob (IOOperation.Read, s => ((SocketAsyncResult) s).Worker.Receive (), sockares));
1736
1737                         return true;
1738                 }
1739
1740                 public IAsyncResult BeginReceive (byte[] buffer, int offset, int size, SocketFlags socket_flags, AsyncCallback callback, object state)
1741                 {
1742                         ThrowIfDisposedAndClosed ();
1743                         ThrowIfBufferNull (buffer);
1744                         ThrowIfBufferOutOfRange (buffer, offset, size);
1745
1746                         SocketAsyncResult sockares = new SocketAsyncResult (this, callback, state, SocketOperation.Receive) {
1747                                 Buffer = buffer,
1748                                 Offset = offset,
1749                                 Size = size,
1750                                 SockFlags = socket_flags,
1751                         };
1752
1753                         QueueIOSelectorJob (readQ, sockares.handle, new IOSelectorJob (IOOperation.Read, s => ((SocketAsyncResult) s).Worker.Receive (), sockares));
1754
1755                         return sockares;
1756                 }
1757
1758                 public IAsyncResult BeginReceive (byte[] buffer, int offset, int size, SocketFlags flags, out SocketError error, AsyncCallback callback, object state)
1759                 {
1760                         /* As far as I can tell from the docs and from experimentation, a pointer to the
1761                          * SocketError parameter is not supposed to be saved for the async parts.  And as we don't
1762                          * set any socket errors in the setup code, we just have to set it to Success. */
1763                         error = SocketError.Success;
1764                         return BeginReceive (buffer, offset, size, flags, callback, state);
1765                 }
1766
1767                 [CLSCompliant (false)]
1768                 public IAsyncResult BeginReceive (IList<ArraySegment<byte>> buffers, SocketFlags socketFlags, AsyncCallback callback, object state)
1769                 {
1770                         ThrowIfDisposedAndClosed ();
1771
1772                         if (buffers == null)
1773                                 throw new ArgumentNullException ("buffers");
1774
1775                         SocketAsyncResult sockares = new SocketAsyncResult (this, callback, state, SocketOperation.ReceiveGeneric) {
1776                                 Buffers = buffers,
1777                                 SockFlags = socketFlags,
1778                         };
1779
1780                         QueueIOSelectorJob (readQ, sockares.handle, new IOSelectorJob (IOOperation.Read, s => ((SocketAsyncResult) s).Worker.Receive (), sockares));
1781
1782                         return sockares;
1783                 }
1784
1785                 [CLSCompliant (false)]
1786                 public IAsyncResult BeginReceive (IList<ArraySegment<byte>> buffers, SocketFlags socketFlags, out SocketError errorCode, AsyncCallback callback, object state)
1787                 {
1788                         /* I assume the same SocketError semantics as above */
1789                         errorCode = SocketError.Success;
1790                         return BeginReceive (buffers, socketFlags, callback, state);
1791                 }
1792
1793                 public int EndReceive (IAsyncResult result)
1794                 {
1795                         SocketError error;
1796                         int bytesReceived = EndReceive (result, out error);
1797
1798                         if (error != SocketError.Success) {
1799                                 if (error != SocketError.WouldBlock && error != SocketError.InProgress)
1800                                         is_connected = false;
1801                                 throw new SocketException ((int)error);
1802                         }
1803
1804                         return bytesReceived;
1805                 }
1806
1807                 public int EndReceive (IAsyncResult asyncResult, out SocketError errorCode)
1808                 {
1809                         ThrowIfDisposedAndClosed ();
1810
1811                         SocketAsyncResult sockares = ValidateEndIAsyncResult (asyncResult, "EndReceive", "asyncResult");
1812
1813                         if (!sockares.IsCompleted)
1814                                 sockares.AsyncWaitHandle.WaitOne ();
1815
1816                         // If no socket error occurred, call CheckIfThrowDelayedException in case there are other
1817                         // kinds of exceptions that should be thrown.
1818                         if ((errorCode = sockares.ErrorCode) == SocketError.Success)
1819                                 sockares.CheckIfThrowDelayedException();
1820
1821                         return sockares.Total;
1822                 }
1823
1824                 internal int Receive_nochecks (byte [] buf, int offset, int size, SocketFlags flags, out SocketError error)
1825                 {
1826                         int nativeError;
1827                         int ret = Receive_internal (safe_handle, buf, offset, size, flags, out nativeError);
1828
1829                         error = (SocketError) nativeError;
1830                         if (error != SocketError.Success && error != SocketError.WouldBlock && error != SocketError.InProgress) {
1831                                 is_connected = false;
1832                                 is_bound = false;
1833                         } else {
1834                                 is_connected = true;
1835                         }
1836
1837                         return ret;
1838                 }
1839
1840                 static int Receive_internal (SafeSocketHandle safeHandle, WSABUF[] bufarray, SocketFlags flags, out int error)
1841                 {
1842                         try {
1843                                 safeHandle.RegisterForBlockingSyscall ();
1844                                 return Receive_internal (safeHandle.DangerousGetHandle (), bufarray, flags, out error);
1845                         } finally {
1846                                 safeHandle.UnRegisterForBlockingSyscall ();
1847                         }
1848                 }
1849
1850                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
1851                 extern static int Receive_internal (IntPtr sock, WSABUF[] bufarray, SocketFlags flags, out int error);
1852
1853                 internal static int Receive_internal (SafeSocketHandle safeHandle, byte[] buffer, int offset, int count, SocketFlags flags, out int error)
1854                 {
1855                         try {
1856                                 safeHandle.RegisterForBlockingSyscall ();
1857                                 return Receive_internal (safeHandle.DangerousGetHandle (), buffer, offset, count, flags, out error);
1858                         } finally {
1859                                 safeHandle.UnRegisterForBlockingSyscall ();
1860                         }
1861                 }
1862
1863                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1864                 extern static int Receive_internal(IntPtr sock, byte[] buffer, int offset, int count, SocketFlags flags, out int error);
1865
1866 #endregion
1867
1868 #region ReceiveFrom
1869
1870                 public int ReceiveFrom (byte [] buffer, ref EndPoint remoteEP)
1871                 {
1872                         ThrowIfDisposedAndClosed ();
1873                         ThrowIfBufferNull (buffer);
1874                         ThrowIfBufferOutOfRange (buffer, 0, buffer.Length);
1875
1876                         if (remoteEP == null)
1877                                 throw new ArgumentNullException ("remoteEP");
1878
1879                         return ReceiveFrom_nochecks (buffer, 0, buffer.Length, SocketFlags.None, ref remoteEP);
1880                 }
1881
1882                 public int ReceiveFrom (byte [] buffer, SocketFlags flags, ref EndPoint remoteEP)
1883                 {
1884                         ThrowIfDisposedAndClosed ();
1885                         ThrowIfBufferNull (buffer);
1886                         ThrowIfBufferOutOfRange (buffer, 0, buffer.Length);
1887
1888                         if (remoteEP == null)
1889                                 throw new ArgumentNullException ("remoteEP");
1890
1891                         return ReceiveFrom_nochecks (buffer, 0, buffer.Length, flags, ref remoteEP);
1892                 }
1893
1894                 public int ReceiveFrom (byte [] buffer, int size, SocketFlags flags, ref EndPoint remoteEP)
1895                 {
1896                         ThrowIfDisposedAndClosed ();
1897                         ThrowIfBufferNull (buffer);
1898                         ThrowIfBufferOutOfRange (buffer, 0, size);
1899
1900                         if (remoteEP == null)
1901                                 throw new ArgumentNullException ("remoteEP");
1902
1903                         return ReceiveFrom_nochecks (buffer, 0, size, flags, ref remoteEP);
1904                 }
1905
1906                 public int ReceiveFrom (byte [] buffer, int offset, int size, SocketFlags flags, ref EndPoint remoteEP)
1907                 {
1908                         ThrowIfDisposedAndClosed ();
1909                         ThrowIfBufferNull (buffer);
1910                         ThrowIfBufferOutOfRange (buffer, offset, size);
1911
1912                         if (remoteEP == null)
1913                                 throw new ArgumentNullException ("remoteEP");
1914
1915                         return ReceiveFrom_nochecks (buffer, offset, size, flags, ref remoteEP);
1916                 }
1917
1918                 public bool ReceiveFromAsync (SocketAsyncEventArgs e)
1919                 {
1920                         ThrowIfDisposedAndClosed ();
1921
1922                         // We do not support recv into multiple buffers yet
1923                         if (e.BufferList != null)
1924                                 throw new NotSupportedException ("Mono doesn't support using BufferList at this point.");
1925                         if (e.RemoteEndPoint == null)
1926                                 throw new ArgumentNullException ("remoteEP", "Value cannot be null.");
1927
1928                         e.curSocket = this;
1929                         e.Worker.Init (this, e, SocketOperation.ReceiveFrom);
1930
1931                         SocketAsyncResult sockares = e.Worker.result;
1932                         sockares.Buffer = e.Buffer;
1933                         sockares.Offset = e.Offset;
1934                         sockares.Size = e.Count;
1935                         sockares.EndPoint = e.RemoteEndPoint;
1936                         sockares.SockFlags = e.SocketFlags;
1937
1938                         QueueIOSelectorJob (readQ, sockares.handle, new IOSelectorJob (IOOperation.Read, s => ((SocketAsyncResult) s).Worker.ReceiveFrom (), sockares));
1939
1940                         return true;
1941                 }
1942
1943                 public IAsyncResult BeginReceiveFrom (byte[] buffer, int offset, int size, SocketFlags socket_flags, ref EndPoint remote_end, AsyncCallback callback, object state)
1944                 {
1945                         ThrowIfDisposedAndClosed ();
1946                         ThrowIfBufferNull (buffer);
1947                         ThrowIfBufferOutOfRange (buffer, offset, size);
1948
1949                         if (remote_end == null)
1950                                 throw new ArgumentNullException ("remote_end");
1951
1952                         SocketAsyncResult sockares = new SocketAsyncResult (this, callback, state, SocketOperation.ReceiveFrom) {
1953                                 Buffer = buffer,
1954                                 Offset = offset,
1955                                 Size = size,
1956                                 SockFlags = socket_flags,
1957                                 EndPoint = remote_end,
1958                         };
1959
1960                         QueueIOSelectorJob (readQ, sockares.handle, new IOSelectorJob (IOOperation.Read, s => ((SocketAsyncResult) s).Worker.ReceiveFrom (), sockares));
1961
1962                         return sockares;
1963                 }
1964
1965                 public int EndReceiveFrom(IAsyncResult result, ref EndPoint end_point)
1966                 {
1967                         ThrowIfDisposedAndClosed ();
1968
1969                         if (end_point == null)
1970                                 throw new ArgumentNullException ("remote_end");
1971
1972                         SocketAsyncResult sockares = ValidateEndIAsyncResult (result, "EndReceiveFrom", "result");
1973
1974                         if (!sockares.IsCompleted)
1975                                 sockares.AsyncWaitHandle.WaitOne();
1976
1977                         sockares.CheckIfThrowDelayedException();
1978
1979                         end_point = sockares.EndPoint;
1980
1981                         return sockares.Total;
1982                 }
1983
1984                 internal int ReceiveFrom_nochecks (byte [] buf, int offset, int size, SocketFlags flags, ref EndPoint remote_end)
1985                 {
1986                         int error;
1987                         return ReceiveFrom_nochecks_exc (buf, offset, size, flags, ref remote_end, true, out error);
1988                 }
1989
1990                 internal int ReceiveFrom_nochecks_exc (byte [] buf, int offset, int size, SocketFlags flags, ref EndPoint remote_end, bool throwOnError, out int error)
1991                 {
1992                         SocketAddress sockaddr = remote_end.Serialize();
1993
1994                         int cnt = ReceiveFrom_internal (safe_handle, buf, offset, size, flags, ref sockaddr, out error);
1995
1996                         SocketError err = (SocketError) error;
1997                         if (err != 0) {
1998                                 if (err != SocketError.WouldBlock && err != SocketError.InProgress) {
1999                                         is_connected = false;
2000                                 } else if (err == SocketError.WouldBlock && is_blocking) { // This might happen when ReceiveTimeout is set
2001                                         if (throwOnError)       
2002                                                 throw new SocketException ((int) SocketError.TimedOut, TIMEOUT_EXCEPTION_MSG);
2003                                         error = (int) SocketError.TimedOut;
2004                                         return 0;
2005                                 }
2006
2007                                 if (throwOnError)
2008                                         throw new SocketException (error);
2009
2010                                 return 0;
2011                         }
2012
2013                         is_connected = true;
2014                         is_bound = true;
2015
2016                         /* If sockaddr is null then we're a connection oriented protocol and should ignore the
2017                          * remote_end parameter (see MSDN documentation for Socket.ReceiveFrom(...) ) */
2018                         if (sockaddr != null) {
2019                                 /* Stupidly, EndPoint.Create() is an instance method */
2020                                 remote_end = remote_end.Create (sockaddr);
2021                         }
2022
2023                         seed_endpoint = remote_end;
2024
2025                         return cnt;
2026                 }
2027
2028                 static int ReceiveFrom_internal (SafeSocketHandle safeHandle, byte[] buffer, int offset, int count, SocketFlags flags, ref SocketAddress sockaddr, out int error)
2029                 {
2030                         try {
2031                                 safeHandle.RegisterForBlockingSyscall ();
2032                                 return ReceiveFrom_internal (safeHandle.DangerousGetHandle (), buffer, offset, count, flags, ref sockaddr, out error);
2033                         } finally {
2034                                 safeHandle.UnRegisterForBlockingSyscall ();
2035                         }
2036                 }
2037
2038                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
2039                 extern static int ReceiveFrom_internal(IntPtr sock, byte[] buffer, int offset, int count, SocketFlags flags, ref SocketAddress sockaddr, out int error);
2040
2041 #endregion
2042
2043 #region ReceiveMessageFrom
2044
2045                 [MonoTODO ("Not implemented")]
2046                 public int ReceiveMessageFrom (byte[] buffer, int offset, int size, ref SocketFlags socketFlags, ref EndPoint remoteEP, out IPPacketInformation ipPacketInformation)
2047                 {
2048                         ThrowIfDisposedAndClosed ();
2049                         ThrowIfBufferNull (buffer);
2050                         ThrowIfBufferOutOfRange (buffer, offset, size);
2051
2052                         if (remoteEP == null)
2053                                 throw new ArgumentNullException ("remoteEP");
2054
2055                         // FIXME: figure out how we get hold of the IPPacketInformation
2056                         throw new NotImplementedException ();
2057                 }
2058
2059                 [MonoTODO ("Not implemented")]
2060                 public bool ReceiveMessageFromAsync (SocketAsyncEventArgs e)
2061                 {
2062                         // NO check is made whether e != null in MS.NET (NRE is thrown in such case)
2063
2064                         ThrowIfDisposedAndClosed ();
2065
2066                         throw new NotImplementedException ();
2067                 }
2068
2069                 [MonoTODO]
2070                 public IAsyncResult BeginReceiveMessageFrom (byte[] buffer, int offset, int size, SocketFlags socketFlags, ref EndPoint remoteEP, AsyncCallback callback, object state)
2071                 {
2072                         ThrowIfDisposedAndClosed ();
2073                         ThrowIfBufferNull (buffer);
2074                         ThrowIfBufferOutOfRange (buffer, offset, size);
2075
2076                         if (remoteEP == null)
2077                                 throw new ArgumentNullException ("remoteEP");
2078
2079                         throw new NotImplementedException ();
2080                 }
2081
2082                 [MonoTODO]
2083                 public int EndReceiveMessageFrom (IAsyncResult asyncResult, ref SocketFlags socketFlags, ref EndPoint endPoint, out IPPacketInformation ipPacketInformation)
2084                 {
2085                         ThrowIfDisposedAndClosed ();
2086
2087                         if (endPoint == null)
2088                                 throw new ArgumentNullException ("endPoint");
2089
2090                         SocketAsyncResult sockares = ValidateEndIAsyncResult (asyncResult, "EndReceiveMessageFrom", "asyncResult");
2091
2092                         throw new NotImplementedException ();
2093                 }
2094
2095 #endregion
2096
2097 #region Send
2098
2099                 public int Send (byte [] buffer)
2100                 {
2101                         ThrowIfDisposedAndClosed ();
2102                         ThrowIfBufferNull (buffer);
2103                         ThrowIfBufferOutOfRange (buffer, 0, buffer.Length);
2104
2105                         SocketError error;
2106                         int ret = Send_nochecks (buffer, 0, buffer.Length, SocketFlags.None, out error);
2107
2108                         if (error != SocketError.Success)
2109                                 throw new SocketException ((int) error);
2110
2111                         return ret;
2112                 }
2113
2114                 public int Send (byte [] buffer, SocketFlags flags)
2115                 {
2116                         ThrowIfDisposedAndClosed ();
2117                         ThrowIfBufferNull (buffer);
2118                         ThrowIfBufferOutOfRange (buffer, 0, buffer.Length);
2119
2120                         SocketError error;
2121                         int ret = Send_nochecks (buffer, 0, buffer.Length, flags, out error);
2122
2123                         if (error != SocketError.Success)
2124                                 throw new SocketException ((int) error);
2125
2126                         return ret;
2127                 }
2128
2129                 public int Send (byte [] buffer, int size, SocketFlags flags)
2130                 {
2131                         ThrowIfDisposedAndClosed ();
2132                         ThrowIfBufferNull (buffer);
2133                         ThrowIfBufferOutOfRange (buffer, 0, size);
2134
2135                         SocketError error;
2136                         int ret = Send_nochecks (buffer, 0, size, flags, out error);
2137
2138                         if (error != SocketError.Success)
2139                                 throw new SocketException ((int) error);
2140
2141                         return ret;
2142                 }
2143
2144                 public int Send (byte [] buffer, int offset, int size, SocketFlags flags)
2145                 {
2146                         ThrowIfDisposedAndClosed ();
2147                         ThrowIfBufferNull (buffer);
2148                         ThrowIfBufferOutOfRange (buffer, offset, size);
2149
2150                         SocketError error;
2151                         int ret = Send_nochecks (buffer, offset, size, flags, out error);
2152
2153                         if (error != SocketError.Success)
2154                                 throw new SocketException ((int) error);
2155
2156                         return ret;
2157                 }
2158
2159                 public int Send (byte [] buffer, int offset, int size, SocketFlags flags, out SocketError error)
2160                 {
2161                         ThrowIfDisposedAndClosed ();
2162                         ThrowIfBufferNull (buffer);
2163                         ThrowIfBufferOutOfRange (buffer, offset, size);
2164
2165                         return Send_nochecks (buffer, offset, size, flags, out error);
2166                 }
2167
2168                 public
2169                 int Send (IList<ArraySegment<byte>> buffers)
2170                 {
2171                         SocketError error;
2172                         int ret = Send (buffers, SocketFlags.None, out error);
2173
2174                         if (error != SocketError.Success)
2175                                 throw new SocketException ((int) error);
2176
2177                         return ret;
2178                 }
2179
2180                 public
2181                 int Send (IList<ArraySegment<byte>> buffers, SocketFlags socketFlags)
2182                 {
2183                         SocketError error;
2184                         int ret = Send (buffers, socketFlags, out error);
2185
2186                         if (error != SocketError.Success)
2187                                 throw new SocketException ((int) error);
2188
2189                         return ret;
2190                 }
2191
2192                 [CLSCompliant (false)]
2193                 public int Send (IList<ArraySegment<byte>> buffers, SocketFlags socketFlags, out SocketError errorCode)
2194                 {
2195                         ThrowIfDisposedAndClosed ();
2196
2197                         if (buffers == null)
2198                                 throw new ArgumentNullException ("buffers");
2199                         if (buffers.Count == 0)
2200                                 throw new ArgumentException ("Buffer is empty", "buffers");
2201
2202                         int numsegments = buffers.Count;
2203                         int nativeError;
2204                         int ret;
2205
2206                         WSABUF[] bufarray = new WSABUF[numsegments];
2207                         GCHandle[] gch = new GCHandle[numsegments];
2208
2209                         for(int i = 0; i < numsegments; i++) {
2210                                 ArraySegment<byte> segment = buffers[i];
2211
2212                                 if (segment.Offset < 0 || segment.Count < 0 || segment.Count > segment.Array.Length - segment.Offset)
2213                                         throw new ArgumentOutOfRangeException ("segment");
2214
2215                                 gch[i] = GCHandle.Alloc (segment.Array, GCHandleType.Pinned);
2216                                 bufarray[i].len = segment.Count;
2217                                 bufarray[i].buf = Marshal.UnsafeAddrOfPinnedArrayElement (segment.Array, segment.Offset);
2218                         }
2219
2220                         try {
2221                                 ret = Send_internal (safe_handle, bufarray, socketFlags, out nativeError);
2222                         } finally {
2223                                 for(int i = 0; i < numsegments; i++) {
2224                                         if (gch[i].IsAllocated) {
2225                                                 gch[i].Free ();
2226                                         }
2227                                 }
2228                         }
2229
2230                         errorCode = (SocketError)nativeError;
2231
2232                         return ret;
2233                 }
2234
2235                 internal int Send_nochecks (byte [] buf, int offset, int size, SocketFlags flags, out SocketError error)
2236                 {
2237                         if (size == 0) {
2238                                 error = SocketError.Success;
2239                                 return 0;
2240                         }
2241
2242                         int nativeError;
2243                         int ret = Send_internal (safe_handle, buf, offset, size, flags, out nativeError);
2244
2245                         error = (SocketError)nativeError;
2246
2247                         if (error != SocketError.Success && error != SocketError.WouldBlock && error != SocketError.InProgress) {
2248                                 is_connected = false;
2249                                 is_bound = false;
2250                         } else {
2251                                 is_connected = true;
2252                         }
2253
2254                         return ret;
2255                 }
2256
2257                 public bool SendAsync (SocketAsyncEventArgs e)
2258                 {
2259                         // NO check is made whether e != null in MS.NET (NRE is thrown in such case)
2260
2261                         ThrowIfDisposedAndClosed ();
2262
2263                         if (e.Buffer == null && e.BufferList == null)
2264                                 throw new NullReferenceException ("Either e.Buffer or e.BufferList must be valid buffers.");
2265
2266                         e.curSocket = this;
2267                         e.Worker.Init (this, e, e.Buffer != null ? SocketOperation.Send : SocketOperation.SendGeneric);
2268
2269                         SocketAsyncResult sockares = e.Worker.result;
2270                         sockares.SockFlags = e.SocketFlags;
2271
2272                         if (e.Buffer != null) {
2273                                 sockares.Buffer = e.Buffer;
2274                                 sockares.Offset = e.Offset;
2275                                 sockares.Size = e.Count;
2276                         } else {
2277                                 sockares.Buffers = e.BufferList;
2278                         }
2279
2280                         QueueIOSelectorJob (writeQ, sockares.handle, new IOSelectorJob (IOOperation.Write, s => ((SocketAsyncResult) s).Worker.Send (), sockares));
2281
2282                         return true;
2283                 }
2284
2285                 public IAsyncResult BeginSend (byte[] buffer, int offset, int size, SocketFlags socketFlags, out SocketError errorCode, AsyncCallback callback, object state)
2286                 {
2287                         if (!is_connected) {
2288                                 errorCode = SocketError.NotConnected;
2289                                 throw new SocketException ((int) errorCode);
2290                         }
2291
2292                         errorCode = SocketError.Success;
2293                         return BeginSend (buffer, offset, size, socketFlags, callback, state);
2294                 }
2295
2296                 public IAsyncResult BeginSend (byte[] buffer, int offset, int size, SocketFlags socket_flags, AsyncCallback callback, object state)
2297                 {
2298                         ThrowIfDisposedAndClosed ();
2299                         ThrowIfBufferNull (buffer);
2300                         ThrowIfBufferOutOfRange (buffer, offset, size);
2301
2302                         if (!is_connected)
2303                                 throw new SocketException ((int)SocketError.NotConnected);
2304
2305                         SocketAsyncResult sockares = new SocketAsyncResult (this, callback, state, SocketOperation.Send) {
2306                                 Buffer = buffer,
2307                                 Offset = offset,
2308                                 Size = size,
2309                                 SockFlags = socket_flags,
2310                         };
2311
2312                         QueueIOSelectorJob (writeQ, sockares.handle, new IOSelectorJob (IOOperation.Write, s => ((SocketAsyncResult) s).Worker.Send (), sockares));
2313
2314                         return sockares;
2315                 }
2316
2317                 public IAsyncResult BeginSend (IList<ArraySegment<byte>> buffers, SocketFlags socketFlags, AsyncCallback callback, object state)
2318                 {
2319                         ThrowIfDisposedAndClosed ();
2320
2321                         if (buffers == null)
2322                                 throw new ArgumentNullException ("buffers");
2323                         if (!is_connected)
2324                                 throw new SocketException ((int)SocketError.NotConnected);
2325
2326                         SocketAsyncResult sockares = new SocketAsyncResult (this, callback, state, SocketOperation.SendGeneric) {
2327                                 Buffers = buffers,
2328                                 SockFlags = socketFlags,
2329                         };
2330
2331                         QueueIOSelectorJob (writeQ, sockares.handle, new IOSelectorJob (IOOperation.Write, s => ((SocketAsyncResult) s).Worker.SendGeneric (), sockares));
2332
2333                         return sockares;
2334                 }
2335
2336                 [CLSCompliant (false)]
2337                 public IAsyncResult BeginSend (IList<ArraySegment<byte>> buffers, SocketFlags socketFlags, out SocketError errorCode, AsyncCallback callback, object state)
2338                 {
2339                         if (!is_connected) {
2340                                 errorCode = SocketError.NotConnected;
2341                                 throw new SocketException ((int)errorCode);
2342                         }
2343
2344                         errorCode = SocketError.Success;
2345                         return BeginSend (buffers, socketFlags, callback, state);
2346                 }
2347
2348                 public int EndSend (IAsyncResult result)
2349                 {
2350                         SocketError error;
2351                         int bytesSent = EndSend (result, out error);
2352
2353                         if (error != SocketError.Success) {
2354                                 if (error != SocketError.WouldBlock && error != SocketError.InProgress)
2355                                         is_connected = false;
2356                                 throw new SocketException ((int)error);
2357                         }
2358
2359                         return bytesSent;
2360                 }
2361
2362                 public int EndSend (IAsyncResult asyncResult, out SocketError errorCode)
2363                 {
2364                         ThrowIfDisposedAndClosed ();
2365
2366                         SocketAsyncResult sockares = ValidateEndIAsyncResult (asyncResult, "EndSend", "asyncResult");
2367
2368                         if (!sockares.IsCompleted)
2369                                 sockares.AsyncWaitHandle.WaitOne ();
2370
2371                         /* If no socket error occurred, call CheckIfThrowDelayedException in
2372                          * case there are other kinds of exceptions that should be thrown.*/
2373                         if ((errorCode = sockares.ErrorCode) == SocketError.Success)
2374                                 sockares.CheckIfThrowDelayedException ();
2375
2376                         return sockares.Total;
2377                 }
2378
2379                 static int Send_internal (SafeSocketHandle safeHandle, WSABUF[] bufarray, SocketFlags flags, out int error)
2380                 {
2381                         bool release = false;
2382                         try {
2383                                 safeHandle.DangerousAddRef (ref release);
2384                                 return Send_internal (safeHandle.DangerousGetHandle (), bufarray, flags, out error);
2385                         } finally {
2386                                 if (release)
2387                                         safeHandle.DangerousRelease ();
2388                         }
2389                 }
2390
2391                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
2392                 extern static int Send_internal (IntPtr sock, WSABUF[] bufarray, SocketFlags flags, out int error);
2393
2394                 internal static int Send_internal (SafeSocketHandle safeHandle, byte[] buf, int offset, int count, SocketFlags flags, out int error)
2395                 {
2396                         try {
2397                                 safeHandle.RegisterForBlockingSyscall ();
2398                                 return Send_internal (safeHandle.DangerousGetHandle (), buf, offset, count, flags, out error);
2399                         } finally {
2400                                 safeHandle.UnRegisterForBlockingSyscall ();
2401                         }
2402                 }
2403
2404                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
2405                 extern static int Send_internal(IntPtr sock, byte[] buf, int offset, int count, SocketFlags flags, out int error);
2406
2407 #endregion
2408
2409 #region SendTo
2410
2411                 public int SendTo (byte [] buffer, EndPoint remote_end)
2412                 {
2413                         ThrowIfDisposedAndClosed ();
2414                         ThrowIfBufferNull (buffer);
2415                         ThrowIfBufferOutOfRange (buffer, 0, buffer.Length);
2416
2417                         if (remote_end == null)
2418                                 throw new ArgumentNullException ("remote_end");
2419
2420                         return SendTo_nochecks (buffer, 0, buffer.Length, SocketFlags.None, remote_end);
2421                 }
2422
2423                 public int SendTo (byte [] buffer, SocketFlags flags, EndPoint remote_end)
2424                 {
2425                         ThrowIfDisposedAndClosed ();
2426                         ThrowIfBufferNull (buffer);
2427                         ThrowIfBufferOutOfRange (buffer, 0, buffer.Length);
2428
2429                         if (remote_end == null)
2430                                 throw new ArgumentNullException ("remote_end");
2431
2432                         return SendTo_nochecks (buffer, 0, buffer.Length, flags, remote_end);
2433                 }
2434
2435                 public int SendTo (byte [] buffer, int size, SocketFlags flags, EndPoint remote_end)
2436                 {
2437                         ThrowIfDisposedAndClosed ();
2438                         ThrowIfBufferNull (buffer);
2439                         ThrowIfBufferOutOfRange (buffer, 0, size);
2440
2441                         if (remote_end == null)
2442                                 throw new ArgumentNullException ("remote_end");
2443
2444                         return SendTo_nochecks (buffer, 0, size, flags, remote_end);
2445                 }
2446
2447                 public int SendTo (byte [] buffer, int offset, int size, SocketFlags flags, EndPoint remote_end)
2448                 {
2449                         ThrowIfDisposedAndClosed ();
2450                         ThrowIfBufferNull (buffer);
2451                         ThrowIfBufferOutOfRange (buffer, offset, size);
2452
2453                         if (remote_end == null)
2454                                 throw new ArgumentNullException("remote_end");
2455
2456                         return SendTo_nochecks (buffer, offset, size, flags, remote_end);
2457                 }
2458
2459                 internal int SendTo_nochecks (byte [] buffer, int offset, int size, SocketFlags flags, EndPoint remote_end)
2460                 {
2461                         int error;
2462                         int ret = SendTo_internal (safe_handle, buffer, offset, size, flags, remote_end.Serialize (), out error);
2463
2464                         SocketError err = (SocketError) error;
2465                         if (err != 0) {
2466                                 if (err != SocketError.WouldBlock && err != SocketError.InProgress)
2467                                         is_connected = false;
2468                                 throw new SocketException (error);
2469                         }
2470
2471                         is_connected = true;
2472                         is_bound = true;
2473                         seed_endpoint = remote_end;
2474
2475                         return ret;
2476                 }
2477
2478                 public bool SendToAsync (SocketAsyncEventArgs e)
2479                 {
2480                         // NO check is made whether e != null in MS.NET (NRE is thrown in such case)
2481
2482                         ThrowIfDisposedAndClosed ();
2483
2484                         if (e.BufferList != null)
2485                                 throw new NotSupportedException ("Mono doesn't support using BufferList at this point.");
2486                         if (e.RemoteEndPoint == null)
2487                                 throw new ArgumentNullException ("remoteEP", "Value cannot be null.");
2488
2489                         e.curSocket = this;
2490                         e.Worker.Init (this, e, SocketOperation.SendTo);
2491
2492                         SocketAsyncResult sockares = e.Worker.result;
2493                         sockares.Buffer = e.Buffer;
2494                         sockares.Offset = e.Offset;
2495                         sockares.Size = e.Count;
2496                         sockares.SockFlags = e.SocketFlags;
2497                         sockares.EndPoint = e.RemoteEndPoint;
2498
2499                         QueueIOSelectorJob (writeQ, sockares.handle, new IOSelectorJob (IOOperation.Write, s => ((SocketAsyncResult) s).Worker.SendTo (), sockares));
2500
2501                         return true;
2502                 }
2503
2504
2505                 public IAsyncResult BeginSendTo(byte[] buffer, int offset, int size, SocketFlags socket_flags, EndPoint remote_end, AsyncCallback callback, object state)
2506                 {
2507                         ThrowIfDisposedAndClosed ();
2508                         ThrowIfBufferNull (buffer);
2509                         ThrowIfBufferOutOfRange (buffer, offset, size);
2510
2511                         SocketAsyncResult sockares = new SocketAsyncResult (this, callback, state, SocketOperation.SendTo) {
2512                                 Buffer = buffer,
2513                                 Offset = offset,
2514                                 Size = size,
2515                                 SockFlags = socket_flags,
2516                                 EndPoint = remote_end,
2517                         };
2518
2519                         QueueIOSelectorJob (writeQ, sockares.handle, new IOSelectorJob (IOOperation.Write, s => ((SocketAsyncResult) s).Worker.SendTo (), sockares));
2520
2521                         return sockares;
2522                 }
2523
2524                 public int EndSendTo (IAsyncResult result)
2525                 {
2526                         ThrowIfDisposedAndClosed ();
2527
2528                         SocketAsyncResult sockares = ValidateEndIAsyncResult (result, "EndSendTo", "result");
2529
2530                         if (!sockares.IsCompleted)
2531                                 sockares.AsyncWaitHandle.WaitOne();
2532
2533                         sockares.CheckIfThrowDelayedException();
2534
2535                         return sockares.Total;
2536                 }
2537
2538                 static int SendTo_internal (SafeSocketHandle safeHandle, byte[] buffer, int offset, int count, SocketFlags flags, SocketAddress sa, out int error)
2539                 {
2540                         try {
2541                                 safeHandle.RegisterForBlockingSyscall ();
2542                                 return SendTo_internal (safeHandle.DangerousGetHandle (), buffer, offset, count, flags, sa, out error);
2543                         } finally {
2544                                 safeHandle.UnRegisterForBlockingSyscall ();
2545                         }
2546                 }
2547
2548                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
2549                 extern static int SendTo_internal (IntPtr sock, byte[] buffer, int offset, int count, SocketFlags flags, SocketAddress sa, out int error);
2550
2551 #endregion
2552
2553 #region SendFile
2554
2555                 public void SendFile (string fileName)
2556                 {
2557                         ThrowIfDisposedAndClosed ();
2558
2559                         if (!is_connected)
2560                                 throw new NotSupportedException ();
2561                         if (!is_blocking)
2562                                 throw new InvalidOperationException ();
2563
2564                         SendFile (fileName, null, null, 0);
2565                 }
2566
2567                 public void SendFile (string fileName, byte[] preBuffer, byte[] postBuffer, TransmitFileOptions flags)
2568                 {
2569                         ThrowIfDisposedAndClosed ();
2570
2571                         if (!is_connected)
2572                                 throw new NotSupportedException ();
2573                         if (!is_blocking)
2574                                 throw new InvalidOperationException ();
2575
2576                         if (!SendFile_internal (safe_handle, fileName, preBuffer, postBuffer, flags)) {
2577                                 SocketException exc = new SocketException ();
2578                                 if (exc.ErrorCode == 2 || exc.ErrorCode == 3)
2579                                         throw new FileNotFoundException ();
2580                                 throw exc;
2581                         }
2582                 }
2583
2584                 public IAsyncResult BeginSendFile (string fileName, AsyncCallback callback, object state)
2585                 {
2586                         ThrowIfDisposedAndClosed ();
2587
2588                         if (!is_connected)
2589                                 throw new NotSupportedException ();
2590                         if (!File.Exists (fileName))
2591                                 throw new FileNotFoundException ();
2592
2593                         return BeginSendFile (fileName, null, null, 0, callback, state);
2594                 }
2595
2596                 public IAsyncResult BeginSendFile (string fileName, byte[] preBuffer, byte[] postBuffer, TransmitFileOptions flags, AsyncCallback callback, object state)
2597                 {
2598                         ThrowIfDisposedAndClosed ();
2599
2600                         if (!is_connected)
2601                                 throw new NotSupportedException ();
2602                         if (!File.Exists (fileName))
2603                                 throw new FileNotFoundException ();
2604
2605                         SendFileHandler handler = new SendFileHandler (SendFile);
2606
2607                         return new SendFileAsyncResult (handler, handler.BeginInvoke (fileName, preBuffer, postBuffer, flags, ar => callback (new SendFileAsyncResult (handler, ar)), state));
2608                 }
2609
2610                 public void EndSendFile (IAsyncResult asyncResult)
2611                 {
2612                         ThrowIfDisposedAndClosed ();
2613
2614                         if (asyncResult == null)
2615                                 throw new ArgumentNullException ("asyncResult");
2616
2617                         SendFileAsyncResult ares = asyncResult as SendFileAsyncResult;
2618                         if (ares == null)
2619                                 throw new ArgumentException ("Invalid IAsyncResult", "asyncResult");
2620
2621                         ares.Delegate.EndInvoke (ares.Original);
2622                 }
2623
2624                 static bool SendFile_internal (SafeSocketHandle safeHandle, string filename, byte [] pre_buffer, byte [] post_buffer, TransmitFileOptions flags)
2625                 {
2626                         try {
2627                                 safeHandle.RegisterForBlockingSyscall ();
2628                                 return SendFile_internal (safeHandle.DangerousGetHandle (), filename, pre_buffer, post_buffer, flags);
2629                         } finally {
2630                                 safeHandle.UnRegisterForBlockingSyscall ();
2631                         }
2632                 }
2633
2634                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
2635                 extern static bool SendFile_internal (IntPtr sock, string filename, byte [] pre_buffer, byte [] post_buffer, TransmitFileOptions flags);
2636
2637                 delegate void SendFileHandler (string fileName, byte [] preBuffer, byte [] postBuffer, TransmitFileOptions flags);
2638
2639                 sealed class SendFileAsyncResult : IAsyncResult {
2640                         IAsyncResult ares;
2641                         SendFileHandler d;
2642
2643                         public SendFileAsyncResult (SendFileHandler d, IAsyncResult ares)
2644                         {
2645                                 this.d = d;
2646                                 this.ares = ares;
2647                         }
2648
2649                         public object AsyncState {
2650                                 get { return ares.AsyncState; }
2651                         }
2652
2653                         public WaitHandle AsyncWaitHandle {
2654                                 get { return ares.AsyncWaitHandle; }
2655                         }
2656
2657                         public bool CompletedSynchronously {
2658                                 get { return ares.CompletedSynchronously; }
2659                         }
2660
2661                         public bool IsCompleted {
2662                                 get { return ares.IsCompleted; }
2663                         }
2664
2665                         public SendFileHandler Delegate {
2666                                 get { return d; }
2667                         }
2668
2669                         public IAsyncResult Original {
2670                                 get { return ares; }
2671                         }
2672                 }
2673
2674 #endregion
2675
2676 #region SendPackets
2677
2678                 [MonoTODO ("Not implemented")]
2679                 public bool SendPacketsAsync (SocketAsyncEventArgs e)
2680                 {
2681                         // NO check is made whether e != null in MS.NET (NRE is thrown in such case)
2682
2683                         ThrowIfDisposedAndClosed ();
2684
2685                         throw new NotImplementedException ();
2686                 }
2687
2688 #endregion
2689
2690 #region DuplicateAndClose
2691
2692 #if !MOBILE
2693                 [MonoLimitation ("We do not support passing sockets across processes, we merely allow this API to pass the socket across AppDomains")]
2694                 public SocketInformation DuplicateAndClose (int targetProcessId)
2695                 {
2696                         var si = new SocketInformation ();
2697                         si.Options =
2698                                 (is_listening      ? SocketInformationOptions.Listening : 0) |
2699                                 (is_connected      ? SocketInformationOptions.Connected : 0) |
2700                                 (is_blocking       ? 0 : SocketInformationOptions.NonBlocking) |
2701                                 (use_overlapped_io ? SocketInformationOptions.UseOnlyOverlappedIO : 0);
2702
2703                         si.ProtocolInformation = Mono.DataConverter.Pack ("iiiil", (int)address_family, (int)socket_type, (int)protocol_type, is_bound ? 1 : 0, (long)Handle);
2704                         safe_handle = null;
2705
2706                         return si;
2707                 }
2708 #endif
2709
2710 #endregion
2711
2712 #region GetSocketOption
2713
2714                 public void GetSocketOption (SocketOptionLevel optionLevel, SocketOptionName optionName, byte [] optionValue)
2715                 {
2716                         ThrowIfDisposedAndClosed ();
2717
2718                         if (optionValue == null)
2719                                 throw new SocketException ((int) SocketError.Fault, "Error trying to dereference an invalid pointer");
2720
2721                         int error;
2722                         GetSocketOption_arr_internal (safe_handle, optionLevel, optionName, ref optionValue, out error);
2723
2724                         if (error != 0)
2725                                 throw new SocketException (error);
2726                 }
2727
2728                 public byte [] GetSocketOption (SocketOptionLevel optionLevel, SocketOptionName optionName, int length)
2729                 {
2730                         ThrowIfDisposedAndClosed ();
2731
2732                         int error;
2733                         byte[] byte_val = new byte [length];
2734                         GetSocketOption_arr_internal (safe_handle, optionLevel, optionName, ref byte_val, out error);
2735
2736                         if (error != 0)
2737                                 throw new SocketException (error);
2738
2739                         return byte_val;
2740                 }
2741
2742                 public object GetSocketOption (SocketOptionLevel optionLevel, SocketOptionName optionName)
2743                 {
2744                         ThrowIfDisposedAndClosed ();
2745
2746                         int error;
2747                         object obj_val;
2748                         GetSocketOption_obj_internal (safe_handle, optionLevel, optionName, out obj_val, out error);
2749
2750                         if (error != 0)
2751                                 throw new SocketException (error);
2752
2753                         if (optionName == SocketOptionName.Linger)
2754                                 return (LingerOption) obj_val;
2755                         else if (optionName == SocketOptionName.AddMembership || optionName == SocketOptionName.DropMembership)
2756                                 return (MulticastOption) obj_val;
2757                         else if (obj_val is int)
2758                                 return (int) obj_val;
2759                         else
2760                                 return obj_val;
2761                 }
2762
2763                 static void GetSocketOption_arr_internal (SafeSocketHandle safeHandle, SocketOptionLevel level, SocketOptionName name, ref byte[] byte_val, out int error)
2764                 {
2765                         bool release = false;
2766                         try {
2767                                 safeHandle.DangerousAddRef (ref release);
2768                                 GetSocketOption_arr_internal (safeHandle.DangerousGetHandle (), level, name, ref byte_val, out error);
2769                         } finally {
2770                                 if (release)
2771                                         safeHandle.DangerousRelease ();
2772                         }
2773                 }
2774
2775                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
2776                 extern static void GetSocketOption_arr_internal(IntPtr socket, SocketOptionLevel level, SocketOptionName name, ref byte[] byte_val, out int error);
2777
2778                 static void GetSocketOption_obj_internal (SafeSocketHandle safeHandle, SocketOptionLevel level, SocketOptionName name, out object obj_val, out int error)
2779                 {
2780                         bool release = false;
2781                         try {
2782                                 safeHandle.DangerousAddRef (ref release);
2783                                 GetSocketOption_obj_internal (safeHandle.DangerousGetHandle (), level, name, out obj_val, out error);
2784                         } finally {
2785                                 if (release)
2786                                         safeHandle.DangerousRelease ();
2787                         }
2788                 }
2789
2790                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
2791                 extern static void GetSocketOption_obj_internal(IntPtr socket, SocketOptionLevel level, SocketOptionName name, out object obj_val, out int error);
2792
2793 #endregion
2794
2795 #region SetSocketOption
2796
2797                 public void SetSocketOption (SocketOptionLevel optionLevel, SocketOptionName optionName, byte [] optionValue)
2798                 {
2799                         ThrowIfDisposedAndClosed ();
2800
2801                         // I'd throw an ArgumentNullException, but this is what MS does.
2802                         if (optionValue == null)
2803                                 throw new SocketException ((int) SocketError.Fault, "Error trying to dereference an invalid pointer");
2804
2805                         int error;
2806                         SetSocketOption_internal (safe_handle, optionLevel, optionName, null, optionValue, 0, out error);
2807
2808                         if (error != 0) {
2809                                 if (error == (int) SocketError.InvalidArgument)
2810                                         throw new ArgumentException ();
2811                                 throw new SocketException (error);
2812                         }
2813                 }
2814
2815                 public void SetSocketOption (SocketOptionLevel optionLevel, SocketOptionName optionName, object optionValue)
2816                 {
2817                         ThrowIfDisposedAndClosed ();
2818
2819                         // NOTE: if a null is passed, the byte[] overload is used instead...
2820                         if (optionValue == null)
2821                                 throw new ArgumentNullException("optionValue");
2822
2823                         int error;
2824
2825                         if (optionLevel == SocketOptionLevel.Socket && optionName == SocketOptionName.Linger) {
2826                                 LingerOption linger = optionValue as LingerOption;
2827                                 if (linger == null)
2828                                         throw new ArgumentException ("A 'LingerOption' value must be specified.", "optionValue");
2829                                 SetSocketOption_internal (safe_handle, optionLevel, optionName, linger, null, 0, out error);
2830                         } else if (optionLevel == SocketOptionLevel.IP && (optionName == SocketOptionName.AddMembership || optionName == SocketOptionName.DropMembership)) {
2831                                 MulticastOption multicast = optionValue as MulticastOption;
2832                                 if (multicast == null)
2833                                         throw new ArgumentException ("A 'MulticastOption' value must be specified.", "optionValue");
2834                                 SetSocketOption_internal (safe_handle, optionLevel, optionName, multicast, null, 0, out error);
2835                         } else if (optionLevel == SocketOptionLevel.IPv6 && (optionName == SocketOptionName.AddMembership || optionName == SocketOptionName.DropMembership)) {
2836                                 IPv6MulticastOption multicast = optionValue as IPv6MulticastOption;
2837                                 if (multicast == null)
2838                                         throw new ArgumentException ("A 'IPv6MulticastOption' value must be specified.", "optionValue");
2839                                 SetSocketOption_internal (safe_handle, optionLevel, optionName, multicast, null, 0, out error);
2840                         } else {
2841                                 throw new ArgumentException ("Invalid value specified.", "optionValue");
2842                         }
2843
2844                         if (error != 0) {
2845                                 if (error == (int) SocketError.InvalidArgument)
2846                                         throw new ArgumentException ();
2847                                 throw new SocketException (error);
2848                         }
2849                 }
2850
2851                 public void SetSocketOption (SocketOptionLevel optionLevel, SocketOptionName optionName, bool optionValue)
2852                 {
2853                         ThrowIfDisposedAndClosed ();
2854
2855                         int error;
2856                         int int_val = optionValue ? 1 : 0;
2857                         SetSocketOption_internal (safe_handle, optionLevel, optionName, null, null, int_val, out error);
2858
2859                         if (error != 0) {
2860                                 if (error == (int) SocketError.InvalidArgument)
2861                                         throw new ArgumentException ();
2862                                 throw new SocketException (error);
2863                         }
2864                 }
2865
2866                 public void SetSocketOption (SocketOptionLevel optionLevel, SocketOptionName optionName, int optionValue)
2867                 {
2868                         ThrowIfDisposedAndClosed ();
2869
2870                         int error;
2871                         SetSocketOption_internal (safe_handle, optionLevel, optionName, null, null, optionValue, out error);
2872
2873                         if (error != 0) {
2874                                 throw new SocketException (error);
2875                         }
2876                 }
2877
2878                 static void SetSocketOption_internal (SafeSocketHandle safeHandle, SocketOptionLevel level, SocketOptionName name, object obj_val, byte [] byte_val, int int_val, out int error)
2879                 {
2880                         bool release = false;
2881                         try {
2882                                 safeHandle.DangerousAddRef (ref release);
2883                                 SetSocketOption_internal (safeHandle.DangerousGetHandle (), level, name, obj_val, byte_val, int_val, out error);
2884                         } finally {
2885                                 if (release)
2886                                         safeHandle.DangerousRelease ();
2887                         }
2888                 }
2889
2890                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
2891                 extern static void SetSocketOption_internal (IntPtr socket, SocketOptionLevel level, SocketOptionName name, object obj_val, byte [] byte_val, int int_val, out int error);
2892
2893 #endregion
2894
2895 #region IOControl
2896
2897                 public int IOControl (int ioctl_code, byte [] in_value, byte [] out_value)
2898                 {
2899                         if (is_disposed)
2900                                 throw new ObjectDisposedException (GetType ().ToString ());
2901
2902                         int error;
2903                         int result = IOControl_internal (safe_handle, ioctl_code, in_value, out_value, out error);
2904
2905                         if (error != 0)
2906                                 throw new SocketException (error);
2907                         if (result == -1)
2908                                 throw new InvalidOperationException ("Must use Blocking property instead.");
2909
2910                         return result;
2911                 }
2912
2913                 public int IOControl (IOControlCode ioControlCode, byte[] optionInValue, byte[] optionOutValue)
2914                 {
2915                         return IOControl ((int) ioControlCode, optionInValue, optionOutValue);
2916                 }
2917
2918                 static int IOControl_internal (SafeSocketHandle safeHandle, int ioctl_code, byte [] input, byte [] output, out int error)
2919                 {
2920                         bool release = false;
2921                         try {
2922                                 safeHandle.DangerousAddRef (ref release);
2923                                 return IOControl_internal (safeHandle.DangerousGetHandle (), ioctl_code, input, output, out error);
2924                         } finally {
2925                                 if (release)
2926                                         safeHandle.DangerousRelease ();
2927                         }
2928                 }
2929
2930                 /* See Socket.IOControl, WSAIoctl documentation in MSDN. The common options between UNIX
2931                  * and Winsock are FIONREAD, FIONBIO and SIOCATMARK. Anything else will depend on the system
2932                  * except SIO_KEEPALIVE_VALS which is properly handled on both windows and linux. */
2933                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
2934                 extern static int IOControl_internal (IntPtr sock, int ioctl_code, byte [] input, byte [] output, out int error);
2935
2936 #endregion
2937
2938 #region Close
2939
2940                 public void Close ()
2941                 {
2942                         linger_timeout = 0;
2943                         Dispose ();
2944                 }
2945
2946                 public void Close (int timeout)
2947                 {
2948                         linger_timeout = timeout;
2949                         Dispose ();
2950                 }
2951
2952                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
2953                 internal extern static void Close_internal (IntPtr socket, out int error);
2954
2955 #endregion
2956
2957 #region Shutdown
2958
2959                 public void Shutdown (SocketShutdown how)
2960                 {
2961                         ThrowIfDisposedAndClosed ();
2962
2963                         if (!is_connected)
2964                                 throw new SocketException (10057); // Not connected
2965
2966                         int error;
2967                         Shutdown_internal (safe_handle, how, out error);
2968
2969                         if (error != 0)
2970                                 throw new SocketException (error);
2971                 }
2972
2973                 static void Shutdown_internal (SafeSocketHandle safeHandle, SocketShutdown how, out int error)
2974                 {
2975                         bool release = false;
2976                         try {
2977                                 safeHandle.DangerousAddRef (ref release);
2978                                 Shutdown_internal (safeHandle.DangerousGetHandle (), how, out error);
2979                         } finally {
2980                                 if (release)
2981                                         safeHandle.DangerousRelease ();
2982                         }
2983                 }
2984
2985                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
2986                 extern static void Shutdown_internal (IntPtr socket, SocketShutdown how, out int error);
2987
2988 #endregion
2989
2990 #region Dispose
2991
2992                 protected virtual void Dispose (bool disposing)
2993                 {
2994                         if (is_disposed)
2995                                 return;
2996
2997                         is_disposed = true;
2998                         bool was_connected = is_connected;
2999                         is_connected = false;
3000
3001                         if (safe_handle != null) {
3002                                 is_closed = true;
3003                                 IntPtr x = Handle;
3004
3005                                 if (was_connected)
3006                                         Linger (x);
3007
3008                                 safe_handle.Dispose ();
3009                         }
3010                 }
3011
3012                 public void Dispose ()
3013                 {
3014                         Dispose (true);
3015                         GC.SuppressFinalize (this);
3016                 }
3017
3018                 void Linger (IntPtr handle)
3019                 {
3020                         if (!is_connected || linger_timeout <= 0)
3021                                 return;
3022
3023                         /* We don't want to receive any more data */
3024                         int error;
3025                         Shutdown_internal (handle, SocketShutdown.Receive, out error);
3026
3027                         if (error != 0)
3028                                 return;
3029
3030                         int seconds = linger_timeout / 1000;
3031                         int ms = linger_timeout % 1000;
3032                         if (ms > 0) {
3033                                 /* If the other end closes, this will return 'true' with 'Available' == 0 */
3034                                 Poll_internal (handle, SelectMode.SelectRead, ms * 1000, out error);
3035                                 if (error != 0)
3036                                         return;
3037                         }
3038
3039                         if (seconds > 0) {
3040                                 LingerOption linger = new LingerOption (true, seconds);
3041                                 SetSocketOption_internal (handle, SocketOptionLevel.Socket, SocketOptionName.Linger, linger, null, 0, out error);
3042                                 /* Not needed, we're closing upon return */
3043                                 //if (error != 0)
3044                                 //      return;
3045                         }
3046                 }
3047
3048 #endregion
3049
3050                 void ThrowIfDisposedAndClosed (Socket socket)
3051                 {
3052                         if (socket.is_disposed && socket.is_closed)
3053                                 throw new ObjectDisposedException (socket.GetType ().ToString ());
3054                 }
3055
3056                 void ThrowIfDisposedAndClosed ()
3057                 {
3058                         if (is_disposed && is_closed)
3059                                 throw new ObjectDisposedException (GetType ().ToString ());
3060                 }
3061
3062                 void ThrowIfBufferNull (byte[] buffer)
3063                 {
3064                         if (buffer == null)
3065                                 throw new ArgumentNullException ("buffer");
3066                 }
3067
3068                 void ThrowIfBufferOutOfRange (byte[] buffer, int offset, int size)
3069                 {
3070                         if (offset < 0)
3071                                 throw new ArgumentOutOfRangeException ("offset", "offset must be >= 0");
3072                         if (offset > buffer.Length)
3073                                 throw new ArgumentOutOfRangeException ("offset", "offset must be <= buffer.Length");
3074                         if (size < 0)
3075                                 throw new ArgumentOutOfRangeException ("size", "size must be >= 0");
3076                         if (size > buffer.Length - offset)
3077                                 throw new ArgumentOutOfRangeException ("size", "size must be <= buffer.Length - offset");
3078                 }
3079
3080                 void ThrowIfUdp ()
3081                 {
3082 #if !NET_2_1 || MOBILE
3083                         if (protocol_type == ProtocolType.Udp)
3084                                 throw new SocketException ((int)SocketError.ProtocolOption);
3085 #endif
3086                 }
3087
3088                 SocketAsyncResult ValidateEndIAsyncResult (IAsyncResult ares, string methodName, string argName)
3089                 {
3090                         if (ares == null)
3091                                 throw new ArgumentNullException (argName);
3092
3093                         SocketAsyncResult sockares = ares as SocketAsyncResult;
3094                         if (sockares == null)
3095                                 throw new ArgumentException ("Invalid IAsyncResult", argName);
3096                         if (Interlocked.CompareExchange (ref sockares.EndCalled, 1, 0) == 1)
3097                                 throw new InvalidOperationException (methodName + " can only be called once per asynchronous operation");
3098
3099                         return sockares;
3100                 }
3101
3102                 void QueueIOSelectorJob (Queue<KeyValuePair<IntPtr, IOSelectorJob>> queue, IntPtr handle, IOSelectorJob job)
3103                 {
3104                         int count;
3105                         lock (queue) {
3106                                 queue.Enqueue (new KeyValuePair<IntPtr, IOSelectorJob> (handle, job));
3107                                 count = queue.Count;
3108                         }
3109
3110                         if (count == 1)
3111                                 IOSelector.Add (handle, job);
3112                 }
3113
3114                 [StructLayout (LayoutKind.Sequential)]
3115                 struct WSABUF {
3116                         public int len;
3117                         public IntPtr buf;
3118                 }
3119
3120                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
3121                 internal static extern void cancel_blocking_socket_operation (Thread thread);
3122         }
3123 }
3124