~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
/*
  Copyright (C) 2010-2011 Jeroen Frijters

  This software is provided 'as-is', without any express or implied
  warranty.  In no event will the authors be held liable for any damages
  arising from the use of this software.

  Permission is granted to anyone to use this software for any purpose,
  including commercial applications, and to alter it and redistribute it
  freely, subject to the following restrictions:

  1. The origin of this software must not be misrepresented; you must not
     claim that you wrote the original software. If you use this software
     in a product, an acknowledgment in the product documentation would be
     appreciated but is not required.
  2. Altered source versions must be plainly marked as such, and must not be
     misrepresented as being the original software.
  3. This notice may not be removed or altered from any source distribution.

  Jeroen Frijters
  jeroen@frijters.net

*/

package ikvm.internal;

import cli.System.Net.EndPoint;
import cli.System.Net.IPAddress;
import cli.System.Net.IPEndPoint;
import cli.System.Net.Sockets.AddressFamily;
import cli.System.Net.Sockets.IOControlCode;
import cli.System.Net.Sockets.IPv6MulticastOption;
import cli.System.Net.Sockets.LingerOption;
import cli.System.Net.Sockets.MulticastOption;
import cli.System.Net.Sockets.ProtocolType;
import cli.System.Net.Sockets.SocketFlags;
import cli.System.Net.Sockets.SocketOptionName;
import cli.System.Net.Sockets.SocketOptionLevel;
import cli.System.Net.Sockets.SocketShutdown;
import cli.System.Net.Sockets.SocketType;
import ikvm.lang.CIL;

@ikvm.lang.Internal
public final class Winsock
{
    private Winsock() { }

    // remember the last error code
    @cli.System.ThreadStaticAttribute.Annotation
    private static int lastError;

    // Error Codes
    public static final int WSA_NOT_ENOUGH_MEMORY = 8;
    public static final int WSA_OPERATION_ABORTED = 995;
    public static final int WSAEINTR = 10004;
    public static final int WSAEACCES = 10013;
    public static final int WSAEFAULT = 10014;
    public static final int WSAEINVAL = 10022;
    public static final int WSAEMFILE = 10024;
    public static final int WSAEWOULDBLOCK = 10035;
    public static final int WSAEINPROGRESS = 10036;
    public static final int WSAEALREADY = 10037;
    public static final int WSAENOTSOCK = 10038;
    public static final int WSAEDESTADDRREQ = 10039;
    public static final int WSAEMSGSIZE = 10040;
    public static final int WSAEPROTOTYPE = 10041;
    public static final int WSAENOPROTOOPT = 10042;
    public static final int WSAEPROTONOSUPPORT = 10043;
    public static final int WSAESOCKTNOSUPPORT = 10044;
    public static final int WSAEOPNOTSUPP = 10045;
    public static final int WSAEPFNOSUPPORT = 10046;
    public static final int WSAEAFNOSUPPORT = 10047;
    public static final int WSAEADDRINUSE = 10048;
    public static final int WSAEADDRNOTAVAIL = 10049;
    public static final int WSAENETDOWN = 10050;
    public static final int WSAENETUNREACH = 10051;
    public static final int WSAENETRESET = 10052;
    public static final int WSAECONNABORTED = 10053;
    public static final int WSAECONNRESET = 10054;
    public static final int WSAENOBUFS = 10055;
    public static final int WSAEISCONN = 10056;
    public static final int WSAENOTCONN = 10057;
    public static final int WSAESHUTDOWN = 10058;
    public static final int WSAETIMEDOUT = 10060;
    public static final int WSAECONNREFUSED = 10061;
    public static final int WSAEHOSTDOWN = 10064;
    public static final int WSAEHOSTUNREACH = 10065;
    public static final int WSAEPROCLIM = 10067;
    public static final int WSASYSNOTREADY = 10091;
    public static final int WSAVERNOTSUPPORTED = 10092;
    public static final int WSANOTINITIALISED = 10093;
    public static final int WSAEDISCON = 10101;
    public static final int WSATYPE_NOT_FOUND = 10109;
    public static final int WSAHOST_NOT_FOUND = 11001;
    public static final int WSATRY_AGAIN = 11002;
    public static final int WSANO_RECOVERY = 11003;
    public static final int WSANO_DATA = 11004;

    // Other constants
    public static final int SOCKET_ERROR = -1;
    public static final cli.System.Net.Sockets.Socket INVALID_SOCKET = null;

    public static final int AF_INET = AddressFamily.InterNetwork;
    public static final int AF_INET6 = AddressFamily.InterNetworkV6;

    public static final int SOCK_STREAM = SocketType.Stream;
    public static final int SOCK_DGRAM = SocketType.Dgram;

    public static final int SD_RECEIVE = SocketShutdown.Receive;
    public static final int SD_SEND = SocketShutdown.Send;
    public static final int SD_BOTH = SocketShutdown.Both;

    public static final int SOL_SOCKET = SocketOptionLevel.Socket;
    public static final int IPPROTO_TCP = SocketOptionLevel.Tcp;
    public static final int IPPROTO_IP = SocketOptionLevel.IP;
    public static final int IPPROTO_IPV6 = SocketOptionLevel.IPv6;

    public static final int TCP_NODELAY = SocketOptionName.NoDelay;
    public static final int SO_OOBINLINE = SocketOptionName.OutOfBandInline;
    public static final int SO_LINGER = SocketOptionName.Linger;
    public static final int SO_SNDBUF = SocketOptionName.SendBuffer;
    public static final int SO_RCVBUF = SocketOptionName.ReceiveBuffer;
    public static final int SO_KEEPALIVE = SocketOptionName.KeepAlive;
    public static final int SO_REUSEADDR = SocketOptionName.ReuseAddress;
    public static final int SO_BROADCAST = SocketOptionName.Broadcast;
    public static final int SO_RCVTIMEO = SocketOptionName.ReceiveTimeout;
    public static final int SO_ERROR = SocketOptionName.Error;
    public static final int IP_MULTICAST_IF = SocketOptionName.MulticastInterface;
    public static final int IP_MULTICAST_LOOP = SocketOptionName.MulticastLoopback;
    public static final int IP_TOS = SocketOptionName.TypeOfService;
    public static final int IP_MULTICAST_TTL = SocketOptionName.MulticastTimeToLive;
    public static final int IP_ADD_MEMBERSHIP = SocketOptionName.AddMembership;
    public static final int IP_DROP_MEMBERSHIP = SocketOptionName.DropMembership;
    public static final int IPV6_MULTICAST_IF = SocketOptionName.MulticastInterface;
    public static final int IPV6_MULTICAST_LOOP = SocketOptionName.MulticastLoopback;
    public static final int IPV6_MULTICAST_HOPS = SocketOptionName.MulticastTimeToLive;
    public static final int IPV6_ADD_MEMBERSHIP = SocketOptionName.AddMembership;
    public static final int IPV6_DROP_MEMBERSHIP = SocketOptionName.DropMembership;
    public static final int IPV6_V6ONLY = 27;

    public static final int SIO_UDP_CONNRESET = 0x9800000C;

    public static final int MSG_PEEK = SocketFlags.Peek;
    public static final int MSG_OOB = SocketFlags.OutOfBand;

    public static final int FIONREAD = (int)IOControlCode.DataToRead;
    public static final int FIONBIO = (int)IOControlCode.NonBlockingIO;

    public static int WSAGetLastError()
    {
        return lastError;
    }

    public static void WSASetLastError(int err)
    {
        lastError = err;
    }

    public static int WSASendDisconnect(cli.System.Net.Sockets.Socket socket)
    {
        if (socket == null)
        {
            lastError = WSAENOTSOCK;
            return SOCKET_ERROR;
        }
        try
        {
            if (false) throw new cli.System.Net.Sockets.SocketException();
            if (false) throw new cli.System.ObjectDisposedException("");
            socket.Shutdown(SocketShutdown.wrap(SocketShutdown.Send));
            return 0;
        }
        catch (cli.System.Net.Sockets.SocketException x)
        {
            lastError = x.get_ErrorCode();
            return SOCKET_ERROR;
        }
        catch (cli.System.ObjectDisposedException _)
        {
            lastError = WSAENOTSOCK;
            return SOCKET_ERROR;
        }
    }

    public static int WSAIoctl(cli.System.Net.Sockets.Socket socket, int ioControlCode, boolean optionInValue)
    {
        byte[] in = new byte[4];
        in[0] = optionInValue ? (byte)1 : (byte)0;
        byte[] out = new byte[4];
        return WSAIoctl(socket, ioControlCode, in, out);
    }

    public static int WSAIoctl(cli.System.Net.Sockets.Socket socket, int ioControlCode, byte[] optionInValue, byte[] optionOutValue)
    {
        if (socket == null)
        {
            lastError = WSAENOTSOCK;
            return SOCKET_ERROR;
        }
        try
        {
            if (false) throw new cli.System.Net.Sockets.SocketException();
            if (false) throw new cli.System.ObjectDisposedException("");
            if (false) throw new cli.System.InvalidOperationException();
            if (ioControlCode == FIONBIO)
            {
                // it's illegal to meddle with the blocking mode via IOControl
                socket.set_Blocking(optionInValue[0] == 0);
            }
            else if (ioControlCode == FIONREAD)
            {
                int avail = socket.get_Available();
                optionOutValue[0] = (byte)(avail >> 0);
                optionOutValue[1] = (byte)(avail >> 8);
                optionOutValue[2] = (byte)(avail >> 16);
                optionOutValue[3] = (byte)(avail >> 24);
            }
            else
            {
                socket.IOControl(ioControlCode, optionInValue, optionOutValue);
            }
            return 0;
        }
        catch (cli.System.Net.Sockets.SocketException x)
        {
            lastError = x.get_ErrorCode();
            return SOCKET_ERROR;
        }
        catch (cli.System.ObjectDisposedException _)
        {
            lastError = WSAENOTSOCK;
            return SOCKET_ERROR;
        }
        catch (cli.System.InvalidOperationException _)
        {
            lastError = WSAEINVAL;
            return SOCKET_ERROR;
        }
    }

    public static int ioctlsocket(cli.System.Net.Sockets.Socket s, int cmd, int[] argp)
    {
        byte[] in = cli.System.BitConverter.GetBytes(argp[0]);
        byte[] out = new byte[4];
        int ret = WSAIoctl(s, cmd, in, out);
        argp[0] = cli.System.BitConverter.ToInt32(out, 0);
        return ret;
    }

    public static int ioctlsocket(cli.System.Net.Sockets.Socket s, int cmd, int arg)
    {
        byte[] in = cli.System.BitConverter.GetBytes(arg);
        return WSAIoctl(s, cmd, in, in);
    }

    public static cli.System.Net.Sockets.Socket socket(int af, int type, int protocol)
    {
        try
        {
            if (false) throw new cli.System.Net.Sockets.SocketException();
            if (false) throw new cli.System.ObjectDisposedException("");
            return new cli.System.Net.Sockets.Socket(AddressFamily.wrap(af), SocketType.wrap(type), ProtocolType.wrap(protocol));
        }
        catch (cli.System.Net.Sockets.SocketException x)
        {
            lastError = x.get_ErrorCode();
            return INVALID_SOCKET;
        }
        catch (cli.System.ObjectDisposedException _)
        {
            lastError = WSAENOTSOCK;
            return INVALID_SOCKET;
        }
    }

    public static int closesocket(cli.System.Net.Sockets.Socket socket)
    {
        if (socket == null)
        {
            lastError = WSAENOTSOCK;
            return SOCKET_ERROR;
        }
        socket.Close();
        return 0;
    }

    public static final class linger
    {
        public int l_onoff;
        public int l_linger;

        LingerOption ToLingerOption()
        {
            return new LingerOption(l_onoff != 0, l_linger);
        }
    }

    public static final class ip_mreq
    {
        public final in_addr imr_multiaddr = new in_addr();
        public final in_addr imr_interface = new in_addr();

        MulticastOption ToMulticastOption()
        {
            return new MulticastOption(imr_multiaddr.ToIPAddress(), imr_interface.ToIPAddress());
        }
    }

    public static final class in_addr
    {
        public int s_addr;

        IPAddress ToIPAddress()
        {
            return new IPAddress(s_addr & 0xFFFFFFFFL);
        }
    }

    public static final class ipv6_mreq
    {
        public in6_addr ipv6mr_multiaddr;
        public int ipv6mr_interface;

        IPv6MulticastOption ToIPv6MulticastOption()
        {
            return new IPv6MulticastOption(ipv6mr_multiaddr.addr, ipv6mr_interface & 0xFFFFFFFFL);
        }
    }

    public static final class in6_addr
    {
        IPAddress addr;

        public byte[] s6_bytes()
        {
            return addr == null ? new byte[16] : addr.GetAddressBytes();
        }

        public static in6_addr FromSockAddr(IIPEndPointWrapper ep)
        {
            in6_addr addr = new in6_addr();
            addr.addr = ep.get().get_Address();
            return addr;
        }
    }

    public static int getsockopt(cli.System.Net.Sockets.Socket socket, int level, int optname, Object optval)
    {
        if (socket == null)
        {
            lastError = WSAENOTSOCK;
            return SOCKET_ERROR;
        }
        try
        {
            if (false) throw new cli.System.Net.Sockets.SocketException();
            if (false) throw new cli.System.ObjectDisposedException("");
            Object val = socket.GetSocketOption(SocketOptionLevel.wrap(level), SocketOptionName.wrap(optname));
            if (val instanceof cli.System.Int32)
            {
                if (optval instanceof in_addr)
                {
                    ((in_addr)optval).s_addr = CIL.unbox_int(val);
                }
                else
                {
                    ((cli.System.Array)optval).SetValue(val, 0);
                }
            }
            else if (val instanceof LingerOption)
            {
                LingerOption lo = (LingerOption)val;
                linger ling = (linger)optval;
                ling.l_onoff = lo.get_Enabled() ? 1 : 0;
                // FXBUG the linger time is treated as a signed short instead of an unsiged short
                ling.l_linger = lo.get_LingerTime() & 0xFFFF;
            }
            else
            {
                lastError = WSAEINVAL;
                return SOCKET_ERROR;
            }
            return 0;
        }
        catch (cli.System.Net.Sockets.SocketException x)
        {
            lastError = x.get_ErrorCode();
            return SOCKET_ERROR;
        }
        catch (cli.System.ObjectDisposedException _)
        {
            lastError = WSAENOTSOCK;
            return SOCKET_ERROR;
        }
    }

    public static int connect(cli.System.Net.Sockets.Socket socket, IIPEndPointWrapper epw)
    {
        if (socket == null)
        {
            lastError = WSAENOTSOCK;
            return SOCKET_ERROR;
        }
        try
        {
            if (false) throw new cli.System.Net.Sockets.SocketException();
            if (false) throw new cli.System.ObjectDisposedException("");
            IPEndPoint ep = epw.get();
            if (ep == null)
            {
                // it is a disconnect request, we must connect to the Any address
                if (socket.get_AddressFamily().Value == AddressFamily.InterNetwork)
                {
                    ep = new IPEndPoint(cli.System.Net.IPAddress.Any, 0);
                }
                else
                {
                    ep = new IPEndPoint(cli.System.Net.IPAddress.IPv6Any, 0);
                }
            }
            else
            {
                ep = v4mapped(socket, ep);
            }
            if (socket.get_SocketType().Value == SocketType.Dgram)
            {
                // NOTE we use async connect to work around the issue that the .NET Socket class disallows sync Connect after the socket has received WSAECONNRESET
                socket.EndConnect(socket.BeginConnect(ep, null, null));
            }
            else
            {
                socket.Connect(ep);
            }
            return 0;
        }
        catch (cli.System.Net.Sockets.SocketException x)
        {
            lastError = x.get_ErrorCode();
            return SOCKET_ERROR;
        }
        catch (cli.System.ObjectDisposedException _)
        {
            lastError = WSAENOTSOCK;
            return SOCKET_ERROR;
        }
    }

    private static IPEndPoint v4mapped(cli.System.Net.Sockets.Socket socket, IPEndPoint ep)
    {
        // when binding an IPv6 socket to an IPv4 address, we need to use a mapped v4 address
        if (socket.get_AddressFamily().Value == AF_INET6 && ep.get_AddressFamily().Value == AF_INET)
        {
            byte[] v4 = ep.get_Address().GetAddressBytes();
            if (v4[0] == 0 && v4[1] == 0 && v4[2] == 0 && v4[3] == 0)
            {
                return new IPEndPoint(IPAddress.IPv6Any, ep.get_Port());
            }
            else
            {
                byte[] v6 = new byte[16];
                v6[10] = -1;
                v6[11] = -1;
                v6[12] = v4[0];
                v6[13] = v4[1];
                v6[14] = v4[2];
                v6[15] = v4[3];
                return new IPEndPoint(new IPAddress(v6), ep.get_Port());
            }
        }
        return ep;
    }

    public static int bind(cli.System.Net.Sockets.Socket socket, IIPEndPointWrapper ep)
    {
        if (socket == null)
        {
            lastError = WSAENOTSOCK;
            return SOCKET_ERROR;
        }
        try
        {
            if (false) throw new cli.System.Net.Sockets.SocketException();
            if (false) throw new cli.System.ObjectDisposedException("");
            socket.Bind(v4mapped(socket, ep.get()));
            return 0;
        }
        catch (cli.System.Net.Sockets.SocketException x)
        {
            lastError = x.get_ErrorCode();
            return SOCKET_ERROR;
        }
        catch (cli.System.ObjectDisposedException _)
        {
            lastError = WSAENOTSOCK;
            return SOCKET_ERROR;
        }
    }

    public static int listen(cli.System.Net.Sockets.Socket socket, int count)
    {
        if (socket == null)
        {
            lastError = WSAENOTSOCK;
            return SOCKET_ERROR;
        }
        try
        {
            if (false) throw new cli.System.Net.Sockets.SocketException();
            if (false) throw new cli.System.ObjectDisposedException("");
            socket.Listen(count);
            return 0;
        }
        catch (cli.System.Net.Sockets.SocketException x)
        {
            lastError = x.get_ErrorCode();
            return SOCKET_ERROR;
        }
        catch (cli.System.ObjectDisposedException _)
        {
            lastError = WSAENOTSOCK;
            return SOCKET_ERROR;
        }
    }

    public static int shutdown(cli.System.Net.Sockets.Socket socket, int how)
    {
        if (socket == null)
        {
            lastError = WSAENOTSOCK;
            return SOCKET_ERROR;
        }
        try
        {
            if (false) throw new cli.System.Net.Sockets.SocketException();
            if (false) throw new cli.System.ObjectDisposedException("");
            socket.Shutdown(SocketShutdown.wrap(how));
            return 0;
        }
        catch (cli.System.Net.Sockets.SocketException x)
        {
            lastError = x.get_ErrorCode();
            return SOCKET_ERROR;
        }
        catch (cli.System.ObjectDisposedException _)
        {
            lastError = WSAENOTSOCK;
            return SOCKET_ERROR;
        }
    }

    public static final class fd_set
    {
        cli.System.Collections.ArrayList list = new cli.System.Collections.ArrayList();
    }

    public static final class timeval
    {
        public long tv_sec;
        public long tv_usec;
    }

    public static void FD_ZERO(fd_set set)
    {
        set.list.Clear();
    }

    public static void FD_SET(cli.System.Net.Sockets.Socket socket, fd_set set)
    {
        set.list.Add(socket);
    }

    public static boolean FD_ISSET(cli.System.Net.Sockets.Socket socket, fd_set set)
    {
        return set.list.Contains(socket);
    }

    private static cli.System.Collections.ArrayList copy(fd_set set)
    {
        return set == null ? null : (cli.System.Collections.ArrayList)set.list.Clone();
    }

    public static int select(fd_set readfds, fd_set writefds, fd_set exceptfds, timeval timeout)
    {
        long expiration;
        long current = cli.System.DateTime.get_UtcNow().get_Ticks();
        if (timeout == null)
        {
            // FXBUG documentation says that -1 will block forever, but in fact it returns immediately,
            // so we simulate timeout with a large expiration
            expiration = Long.MAX_VALUE;
        }
        else
        {
            long timeout100nanos = Math.min(Long.MAX_VALUE / 10, timeout.tv_usec) * 10 + Math.min(Long.MAX_VALUE / 10000000, timeout.tv_sec) * 10000000;
            expiration = current + Math.min(Long.MAX_VALUE - current, timeout100nanos);
        }
        try
        {
            if (false) throw new cli.System.ArgumentNullException();
            if (false) throw new cli.System.Net.Sockets.SocketException();
            if (false) throw new cli.System.ObjectDisposedException("");
            for (; ; )
            {
                cli.System.Collections.ArrayList checkRead = copy(readfds);
                cli.System.Collections.ArrayList checkWrite = copy(writefds);
                cli.System.Collections.ArrayList checkError = copy(exceptfds);
                int microSeconds = (int)Math.min(Integer.MAX_VALUE, (expiration - current) / 10);
                cli.System.Net.Sockets.Socket.Select(checkRead, checkWrite, checkError, microSeconds);
                int count = 0;
                if (checkRead != null)
                {
                    count += checkRead.get_Count();
                }
                if (checkWrite != null)
                {
                    count += checkWrite.get_Count();
                }
                if (checkError != null)
                {
                    count += checkError.get_Count();
                }
                current = cli.System.DateTime.get_UtcNow().get_Ticks();
                if (count != 0 || current >= expiration)
                {
                    if (readfds != null)
                    {
                        readfds.list = checkRead;
                    }
                    if (writefds != null)
                    {
                        writefds.list = checkWrite;
                    }
                    if (exceptfds != null)
                    {
                        exceptfds.list = checkError;
                    }
                    return count;
                }
            }
        }
        catch (cli.System.ArgumentNullException _)
        {
            lastError = WSAEINVAL;
            return SOCKET_ERROR;
        }
        catch (cli.System.Net.Sockets.SocketException x)
        {
            lastError = x.get_ErrorCode();
            return SOCKET_ERROR;
        }
        catch (cli.System.ObjectDisposedException _)
        {
            lastError = WSAENOTSOCK;
            return SOCKET_ERROR;
        }
    }

    public static int send(cli.System.Net.Sockets.Socket socket, byte[] buf, int len, int flags)
    {
        return send(socket, buf, 0, len, flags);
    }

    public static int send(cli.System.Net.Sockets.Socket socket, byte[] buf, int off, int len, int flags)
    {
        if (socket == null)
        {
            lastError = WSAENOTSOCK;
            return SOCKET_ERROR;
        }
        try
        {
            if (false) throw new cli.System.ArgumentException();
            if (false) throw new cli.System.Net.Sockets.SocketException();
            if (false) throw new cli.System.ObjectDisposedException("");
            return socket.Send(buf, off, len, SocketFlags.wrap(flags));
        }
        catch (cli.System.ArgumentException _)
        {
            lastError = WSAEINVAL;
            return SOCKET_ERROR;
        }
        catch (cli.System.Net.Sockets.SocketException x)
        {
            lastError = x.get_ErrorCode();
            return SOCKET_ERROR;
        }
        catch (cli.System.ObjectDisposedException _)
        {
            lastError = WSAENOTSOCK;
            return SOCKET_ERROR;
        }
    }

    public static int recv(cli.System.Net.Sockets.Socket socket, byte[] buf, int len, int flags)
    {
        return recv(socket, buf, 0, len, flags);
    }

    public static int recv(cli.System.Net.Sockets.Socket socket, byte[] buf, int off, int len, int flags)
    {
        if (socket == null)
        {
            lastError = WSAENOTSOCK;
            return SOCKET_ERROR;
        }
        try
        {
            if (false) throw new cli.System.ArgumentException();
            if (false) throw new cli.System.Net.Sockets.SocketException();
            if (false) throw new cli.System.ObjectDisposedException("");
            return socket.Receive(buf, off, len, SocketFlags.wrap(flags));
        }
        catch (cli.System.ArgumentException _)
        {
            lastError = WSAEINVAL;
            return SOCKET_ERROR;
        }
        catch (cli.System.Net.Sockets.SocketException x)
        {
            lastError = x.get_ErrorCode();
            return SOCKET_ERROR;
        }
        catch (cli.System.ObjectDisposedException _)
        {
            lastError = WSAENOTSOCK;
            return SOCKET_ERROR;
        }
    }

    public static int sendto(cli.System.Net.Sockets.Socket socket, byte[] buf, int off, int len, int flags, IIPEndPointWrapper to)
    {
        if (socket == null)
        {
            lastError = WSAENOTSOCK;
            return SOCKET_ERROR;
        }
        try
        {
            if (false) throw new cli.System.ArgumentException();
            if (false) throw new cli.System.Net.Sockets.SocketException();
            if (false) throw new cli.System.ObjectDisposedException("");
            if (to == null)
            {
                return socket.Send(buf, off, len, SocketFlags.wrap(flags));
            }
            else
            {
                return socket.SendTo(buf, off, len, SocketFlags.wrap(flags), v4mapped(socket, to.get()));
            }
        }
        catch (cli.System.ArgumentException _)
        {
            lastError = WSAEINVAL;
            return SOCKET_ERROR;
        }
        catch (cli.System.Net.Sockets.SocketException x)
        {
            // on Linux we get a WSAECONNREFUSED when sending to an unreachable port/destination, so ignore that
            if (x.get_ErrorCode() == WSAECONNREFUSED)
            {
                return 0;
            }
            lastError = x.get_ErrorCode();
            return SOCKET_ERROR;
        }
        catch (cli.System.ObjectDisposedException _)
        {
            lastError = WSAENOTSOCK;
            return SOCKET_ERROR;
        }
    }

    public static int recvfrom(cli.System.Net.Sockets.Socket socket, byte[] buf, int len, int flags, IIPEndPointWrapper from)
    {
        return recvfrom(socket, buf, 0, len, flags, from);
    }

    public static int recvfrom(cli.System.Net.Sockets.Socket socket, byte[] buf, int off, int len, int flags, IIPEndPointWrapper from)
    {
        if (socket == null)
        {
            lastError = WSAENOTSOCK;
            return SOCKET_ERROR;
        }
        try
        {
            if (false) throw new cli.System.ArgumentException();
            if (false) throw new cli.System.Net.Sockets.SocketException();
            if (false) throw new cli.System.ObjectDisposedException("");
            EndPoint[] ep = new EndPoint[] { socket.get_AddressFamily().Value == AF_INET6 ? new IPEndPoint(IPAddress.IPv6Any, 0) : new IPEndPoint(0, 0) };
            try
            {
                return socket.ReceiveFrom(buf, off, len, SocketFlags.wrap(flags), ep);
            }
            finally
            {
                if (from != null)
                {
                    from.set((IPEndPoint)ep[0]);
                }
            }
        }
        catch (cli.System.ArgumentException _)
        {
            lastError = WSAEINVAL;
            return SOCKET_ERROR;
        }
        catch (cli.System.Net.Sockets.SocketException x)
        {
            lastError = x.get_ErrorCode();
            return SOCKET_ERROR;
        }
        catch (cli.System.ObjectDisposedException _)
        {
            lastError = WSAENOTSOCK;
            return SOCKET_ERROR;
        }
    }

    public static int setsockopt(cli.System.Net.Sockets.Socket s, int level, int optname, Object optval)
    {
        if (s == null)
        {
            lastError = WSAENOTSOCK;
            return SOCKET_ERROR;
        }
        try
        {
            if (false) throw new cli.System.ArgumentException();
            if (false) throw new cli.System.Net.Sockets.SocketException();
            if (false) throw new cli.System.ObjectDisposedException("");
            if (optval instanceof Boolean)
            {
                s.SetSocketOption(SocketOptionLevel.wrap(level), SocketOptionName.wrap(optname), ((Boolean)optval).booleanValue());
            }
            else if (optval instanceof Integer)
            {
                s.SetSocketOption(SocketOptionLevel.wrap(level), SocketOptionName.wrap(optname), ((Integer)optval).intValue());
            }
            else if (optval instanceof linger)
            {
                s.set_LingerState(((linger)optval).ToLingerOption());
            }
            else if (optval instanceof ip_mreq)
            {
                s.SetSocketOption(SocketOptionLevel.wrap(level), SocketOptionName.wrap(optname), ((ip_mreq)optval).ToMulticastOption());
            }
            else if (optval instanceof ipv6_mreq)
            {
                s.SetSocketOption(SocketOptionLevel.wrap(level), SocketOptionName.wrap(optname), ((ipv6_mreq)optval).ToIPv6MulticastOption());
            }
            else if (optval instanceof in_addr)
            {
                s.SetSocketOption(SocketOptionLevel.wrap(level), SocketOptionName.wrap(optname), ((in_addr)optval).s_addr);
            }
            else
            {
                lastError = WSAEINVAL;
                return SOCKET_ERROR;
            }
            return 0;
        }
        catch (cli.System.ArgumentException _)
        {
            lastError = WSAEINVAL;
            return SOCKET_ERROR;
        }
        catch (cli.System.Net.Sockets.SocketException x)
        {
            lastError = x.get_ErrorCode();
            return SOCKET_ERROR;
        }
        catch (cli.System.ObjectDisposedException _)
        {
            lastError = WSAENOTSOCK;
            return SOCKET_ERROR;
        }
    }

    public static cli.System.Net.Sockets.Socket accept(cli.System.Net.Sockets.Socket s, IIPEndPointWrapper ep)
    {
        if (s == null)
        {
            lastError = WSAENOTSOCK;
            return INVALID_SOCKET;
        }
        try
        {
            if (false) throw new cli.System.InvalidOperationException();
            if (false) throw new cli.System.Net.Sockets.SocketException();
            if (false) throw new cli.System.ObjectDisposedException("");
            cli.System.Net.Sockets.Socket remote = s.Accept();
            if (ep != null)
            {
                ep.set((IPEndPoint)remote.get_RemoteEndPoint());
            }
            return remote;
        }
        catch (cli.System.ObjectDisposedException _)
        {
            lastError = WSAENOTSOCK;
            return INVALID_SOCKET;
        }
        catch (cli.System.InvalidOperationException _)
        {
            lastError = WSAEINVAL;
            return INVALID_SOCKET;
        }
        catch (cli.System.Net.Sockets.SocketException x)
        {
            lastError = x.get_ErrorCode();
            return INVALID_SOCKET;
        }
    }

    public interface IIPEndPointWrapper
    {
        void set(IPEndPoint value);
        IPEndPoint get();
    }

    public static int getsockname(cli.System.Net.Sockets.Socket s, IIPEndPointWrapper name)
    {
        if (s == null)
        {
            lastError = WSAENOTSOCK;
            return SOCKET_ERROR;
        }
        try
        {
            if (false) throw new cli.System.Net.Sockets.SocketException();
            if (false) throw new cli.System.ObjectDisposedException("");
            IPEndPoint ep = (IPEndPoint)s.get_LocalEndPoint();
            if (ep == null)
            {
                lastError = WSAEINVAL;
                return SOCKET_ERROR;
            }
            name.set(ep);
            return 0;
        }
        catch (ClassCastException _)
        {
            lastError = WSAEOPNOTSUPP;
            return SOCKET_ERROR;
        }
        catch (cli.System.ObjectDisposedException _)
        {
            lastError = WSAENOTSOCK;
            return SOCKET_ERROR;
        }
        catch (cli.System.Net.Sockets.SocketException x)
        {
            lastError = x.get_ErrorCode();
            return SOCKET_ERROR;
        }
    }

    public static int ntohl(int address)
    {
        return Integer.reverseBytes(address);
    }

    public static int htonl(int address)
    {
        return Integer.reverseBytes(address);
    }

    public static int ntohs(int port)
    {
        return Short.reverseBytes((short)port) & 0xFFFF;
    }

    public static int htons(int port)
    {
        return Short.reverseBytes((short)port) & 0xFFFF;
    }
}