Fix bug #311: On LinkedList.Clear, detach each node instead of dropping them en masse.
[mono.git] / mcs / class / System / System.Net.Sockets / GHStreamSocket.jvm.cs
1 using java.nio.channels;\r
2 using java.security;\r
3 using javax.net.ssl;\r
4 \r
5 namespace System.Net.Sockets\r
6 {\r
7         /// <summary>\r
8         /// Summary description for GHStreamSocket.\r
9         /// </summary>\r
10         internal class GHStreamSocket : GHSocket\r
11         {\r
12                 java.net.ServerSocket jServerSocket;\r
13                 java.net.Socket jSocket;\r
14                 java.nio.channels.ServerSocketChannel jServerSocketChannel;\r
15                 java.nio.channels.SocketChannel jSocketChannel;\r
16 \r
17                 // This field I need because a bug in the java.nio.channels.SocketAdapter, which \r
18                 // returns local port 0 if the socket is not connected (even if the socket is bound)\r
19                 // so I need temporary use regular socket (not channel socket) to bind it to the \r
20                 // local address and use this address in the LocalPoint property and to create the \r
21                 // actual client/server channel sockets\r
22                 // The bug #5076965 (SocketChannel does not report local address after binding to a wildcard )\r
23                 // See: http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5076965\r
24                 java.net.InetSocketAddress jTempLocalSocketAddress;\r
25 \r
26                 public GHStreamSocket()\r
27                 {\r
28                         jSocketChannel = java.nio.channels.SocketChannel.open();\r
29                         jSocket = jSocketChannel.socket();\r
30                 }\r
31 \r
32                 public GHStreamSocket(java.nio.channels.SocketChannel socketChannel)\r
33                 {\r
34                         jSocketChannel = socketChannel;\r
35                         jSocket = jSocketChannel.socket();\r
36                 }\r
37 \r
38                 public override int GetHashCode ()\r
39                 {\r
40                         if (jSocket == null && jServerSocket == null)\r
41                                 return -1;\r
42 \r
43                         if (jServerSocket != null) {\r
44                                 return jServerSocket.ToString ().GetHashCode ();\r
45                         }\r
46 \r
47                         return jSocket.ToString ().GetHashCode ();\r
48                 }\r
49 \r
50                 public int Available_internal(out int error)\r
51                 {\r
52                         error = 0;\r
53                         int r = 0;\r
54 \r
55                         if (jSocket == null || !jSocket.isConnected())\r
56                         {\r
57                                 return r;\r
58                         }\r
59 \r
60                         try\r
61                         {\r
62                                 r = jSocket.getInputStream().available();\r
63                         }\r
64                         catch (Exception e)\r
65                         {\r
66                                 error = 10054; //WSAECONNRESET (Connection reset by peer)\r
67                                 r = 0;\r
68 #if DEBUG\r
69                                 Console.WriteLine("Caught exception during Available_internal - {0}: {1}\n{2}", e.GetType(), e.Message, e.StackTrace);\r
70 #endif\r
71                         }\r
72 \r
73                         return r;\r
74                 }\r
75 \r
76                 public void Blocking_internal(bool block, out int error)\r
77                 {\r
78                         error = 0;\r
79 \r
80                         if (jSocket == null && jServerSocket == null)\r
81                         {\r
82                                 error = 10022; //WSAEINVAL (Invalid argument)\r
83                                 return;\r
84                         }\r
85 \r
86                         try\r
87                         {\r
88                                 if (jServerSocket != null)\r
89                                 {\r
90                                         jServerSocketChannel.configureBlocking(block);\r
91                                 }\r
92                                 else\r
93                                 {\r
94                                         jSocketChannel.configureBlocking(block);\r
95                                 }\r
96                         }\r
97                         catch (Exception e)\r
98                         {\r
99                                 error = 10022; //WSAEINVAL (Invalid argument)\r
100 #if DEBUG\r
101                                 Console.WriteLine("Caught exception during Blocking_internal - {0}: {1}\n{2}", e.GetType(), e.Message, e.StackTrace);\r
102 #endif\r
103                         }\r
104                 }\r
105 \r
106                 public EndPoint LocalEndPoint_internal(out int error)\r
107                 {\r
108                         error = 0;\r
109                         java.net.InetSocketAddress localAddr = null;\r
110 \r
111                         try\r
112                         {\r
113                                 if (jTempLocalSocketAddress != null)\r
114                                 {\r
115                                         localAddr = jTempLocalSocketAddress;\r
116                                 }\r
117                                 else if (jServerSocket != null)\r
118                                 {\r
119                                         localAddr = (java.net.InetSocketAddress)jServerSocket.getLocalSocketAddress();\r
120                                 }\r
121                                 else\r
122                                 {\r
123                                         localAddr = (java.net.InetSocketAddress)jSocket.getLocalSocketAddress();\r
124                                 }\r
125                         }\r
126                         catch (Exception e)\r
127                         {\r
128                                 localAddr = null;\r
129 #if DEBUG\r
130                                 Console.WriteLine("Caught exception during LocalEndPoint_internal - {0}: {1}\n{2}", e.GetType(), e.Message, e.StackTrace);\r
131 #endif\r
132                         }\r
133 \r
134                         if (localAddr == null || localAddr.getAddress() == null || localAddr.getPort() < 0)\r
135                         {\r
136                 return null;\r
137                         }\r
138 \r
139                         IPHostEntry lipa = Dns.Resolve(localAddr.getHostName());\r
140                         IPEndPoint ret = new IPEndPoint(lipa.AddressList[0], localAddr.getPort());\r
141                         return ret;\r
142                 }\r
143 \r
144                 public EndPoint RemoteEndPoint_internal(out int error)\r
145                 {\r
146                         error = 0;\r
147                         java.net.InetSocketAddress remoteAddr = null;\r
148 \r
149                         if (jSocket == null || !jSocket.isBound())\r
150                         {\r
151                                 return null;\r
152                         }\r
153 \r
154                         try\r
155                         {\r
156                                 remoteAddr = (java.net.InetSocketAddress)jSocket.getRemoteSocketAddress();\r
157                         }\r
158                         catch (Exception e)\r
159                         {\r
160                                 remoteAddr = null;\r
161 #if DEBUG\r
162                                 Console.WriteLine("Caught exception during RemoteEndPoint_internal - {0}: {1}\n{2}", e.GetType(), e.Message, e.StackTrace);\r
163 #endif\r
164                         }\r
165 \r
166                         if (remoteAddr == null || remoteAddr.getAddress() == null || remoteAddr.getPort() <= 0)\r
167                         {\r
168                                 error = 10057; //WSAENOTCONN (Socket is not connected)\r
169                                 return null;\r
170                         }\r
171 \r
172                         IPHostEntry lipa = Dns.Resolve(remoteAddr.getHostName());\r
173                         IPEndPoint ret = new IPEndPoint(lipa.AddressList[0], remoteAddr.getPort());\r
174                         return ret;\r
175                 }\r
176 \r
177                 public GHSocket Accept_internal(out int error)\r
178                 {\r
179                         error = 0;\r
180 \r
181                         if (jServerSocket == null)\r
182                         {\r
183                                 throw new InvalidOperationException("You must call Bind and Listen before calling Accept.");\r
184                         }\r
185 \r
186                         try\r
187                         {\r
188                                 /*\r
189                                         If this channel is in non-blocking mode then this method will immediately \r
190                                         return null if there are no pending connections. \r
191                                         Otherwise it will block indefinitely until a new connection is \r
192                                         available or an I/O error occurs.                                \r
193                                 */\r
194                                 java.nio.channels.SocketChannel acceptedSocket = jServerSocketChannel.accept();\r
195                                 if (acceptedSocket == null) \r
196                                 {\r
197                                         error = 10035; //WSAEWOULDBLOCK (Resource temporarily unavailable)\r
198 #if DEBUG\r
199                                         Console.WriteLine("The Accept_internal is in non-blocking mode and no pending connections are available");\r
200 #endif\r
201                                         return null;\r
202                                 }\r
203 \r
204                                 return new GHStreamSocket(acceptedSocket);\r
205                         }\r
206                         catch (AsynchronousCloseException) {\r
207                                 error = 10004;\r
208                         }\r
209                         catch (Exception e)\r
210                         {\r
211                                 error = 10061; //WSAECONNREFUSED (Connection refused)\r
212 #if DEBUG\r
213                                 Console.WriteLine("Caught exception during Accept_internal - {0}: {1}\n{2}", e.GetType(), e.Message, e.StackTrace);\r
214 #endif\r
215                         }\r
216 \r
217                         return null;\r
218                 }\r
219 \r
220                 public void Bind_internal(EndPoint sa, out int error)\r
221                 {\r
222                         error = 0;\r
223                         IPEndPoint addr = sa as IPEndPoint;\r
224                         if (addr == null)\r
225                         {\r
226                                 error = 10044; //WSAESOCKTNOSUPPORT (Socket type not supported)\r
227                                 return;\r
228                         }\r
229 \r
230                         if (jSocket == null || jSocket.isBound() || jSocket.isConnected() || jSocketChannel.isConnectionPending())\r
231                         {\r
232                                 error = 10022; //WSAEINVAL (Invalid argument)\r
233                                 return;\r
234                         }\r
235 \r
236                         try\r
237                         {\r
238                                 // This code I need because a bug in the java.nio.channels.SocketAdapter, which \r
239                                 // returns local port 0 if the socket is not connected (even if the socket is bound)\r
240                                 // so I need temporary use regular socket (not channel socket) to bind it to the \r
241                                 // local address and use this address in the LocalPoint property and to create the \r
242                                 // actual client/server channel sockets\r
243                                 // The bug #5076965 (SocketChannel does not report local address after binding to a wildcard )\r
244                                 // See: http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5076965\r
245                                 java.net.Socket jTempSocket = new java.net.Socket();\r
246                                 jTempSocket.bind(new java.net.InetSocketAddress(java.net.InetAddress.getByName(addr.Address.ToString()),\r
247                                                                                                 addr.Port));\r
248                                 jTempLocalSocketAddress = (java.net.InetSocketAddress)jTempSocket.getLocalSocketAddress();\r
249                                 jTempSocket.close();\r
250                                 jSocket.bind(jTempLocalSocketAddress);\r
251                         }\r
252                         catch (Exception e)\r
253                         {\r
254                                 error = 10048; //WSAEADDRINUSE (Address already in use)\r
255 #if DEBUG\r
256                                 Console.WriteLine("Caught exception during Bind_internal - {0}: {1}\n{2}", e.GetType(), e.Message, e.StackTrace);\r
257 #endif\r
258                         }\r
259                 }\r
260 \r
261                 public void Close_internal(out int error)\r
262                 {\r
263                         error = 0;\r
264 \r
265                         if (jServerSocket != null)\r
266                         {\r
267                                 try\r
268                                 {\r
269                                         jServerSocket.close();\r
270                                 }\r
271                                 catch (Exception e)\r
272                                 {\r
273                                         error = 10022; //WSAEINVAL (Invalid argument)\r
274 #if DEBUG\r
275                                         Console.WriteLine("Caught exception during Close_internal jServerSocket - {0}: {1}\n{2}", e.GetType(), e.Message, e.StackTrace);\r
276 #endif\r
277                                 }\r
278                                 try\r
279                                 {\r
280                                         jServerSocketChannel.close();\r
281                                 }\r
282                                 catch (Exception e)\r
283                                 {\r
284                                         error = 10022; //WSAEINVAL (Invalid argument)\r
285 #if DEBUG\r
286                                         Console.WriteLine("Caught exception during Close_internal jServerSocketChannel - {0}: {1}\n{2}", e.GetType(), e.Message, e.StackTrace);\r
287 #endif\r
288                                 }\r
289                                 jServerSocket = null;\r
290                                 jServerSocketChannel = null;\r
291                         }\r
292                         else if (jSocket != null)\r
293                         {\r
294                                 try\r
295                                 {\r
296                                         jSocket.close();\r
297                                 }\r
298                                 catch (Exception e)\r
299                                 {\r
300                                         error = 10022; //WSAEINVAL (Invalid argument)\r
301 #if DEBUG\r
302                                         Console.WriteLine("Caught exception during Close_internal jSocket - {0}: {1}\n{2}", e.GetType(), e.Message, e.StackTrace);\r
303 #endif\r
304                                 }\r
305                                 try\r
306                                 {\r
307                                         jSocketChannel.close();\r
308                                 }\r
309                                 catch (Exception e)\r
310                                 {\r
311                                         error = 10022; //WSAEINVAL (Invalid argument)\r
312 #if DEBUG\r
313                                         Console.WriteLine("Caught exception during Close_internal jSocketChannel - {0}: {1}\n{2}", e.GetType(), e.Message, e.StackTrace);\r
314 #endif\r
315                                 }\r
316                                 jSocket = null;\r
317                                 jSocketChannel = null;\r
318                         }\r
319                 }\r
320 \r
321                 public void Connect_internal(EndPoint sa, out int error)\r
322                 {\r
323                         error = 0;\r
324 \r
325                         IPEndPoint addr = sa as IPEndPoint;\r
326                         if (addr == null)\r
327                         {\r
328                                 error = 10044; //WSAESOCKTNOSUPPORT (Socket type not supported)\r
329                                 return;\r
330                         }\r
331 \r
332                         if (jSocket == null)\r
333                         {\r
334                                 error = 10022; //WSAEINVAL (Invalid argument)\r
335                                 return;\r
336                         }\r
337 \r
338                         if (jSocket.isConnected() || jSocketChannel.isConnectionPending())\r
339                         {\r
340                                 error = 10056; //WSAEISCONN (Socket is already connected)\r
341                                 return;\r
342                         }\r
343 \r
344                         try\r
345                         {\r
346                                 /*\r
347                                  If this channel is in non-blocking mode then an invocation of this method\r
348                                  initiates a non-blocking connection operation. If the connection is \r
349                                  established immediately, as can happen with a local connection, then this \r
350                                  method returns true. Otherwise this method returns false.  \r
351                  If this channel is in blocking mode then an invocation of this method \r
352                                  will block until the connection is established or an I/O error occurs. \r
353                                  */\r
354                                 bool status = jSocketChannel.connect(new java.net.InetSocketAddress(\r
355                                         java.net.InetAddress.getByName(addr.Address.ToString()), \r
356                                         addr.Port));\r
357                                 if (!status)\r
358                                 {\r
359                                         error = 10035; //WSAEWOULDBLOCK (Resource temporarily unavailable)\r
360                                 }\r
361                         }\r
362                         catch (java.nio.channels.AlreadyConnectedException ae)\r
363                         {                               \r
364                                 error = 10056; //WSAEISCONN (Socket is already connected)\r
365                         }\r
366                         catch (java.nio.channels.ConnectionPendingException cpe)\r
367                         {                               \r
368                                 error = 10036; //WSAEINPROGRESS (Operation now in progress)\r
369                         }\r
370                         catch (java.nio.channels.UnresolvedAddressException uae)\r
371                         {                               \r
372                                 error = 10039; //WSAEDESTADDRREQ (Destination address required)\r
373                         }\r
374                         catch (java.nio.channels.UnsupportedAddressTypeException uate)\r
375                         {                               \r
376                                 error = 10041; //WSAEPROTOTYPE (Protocol wrong type for socket)\r
377                         }\r
378                         catch (AsynchronousCloseException) {\r
379                                 error = 10004;\r
380                         }\r
381                         catch (Exception e)\r
382                         {                               \r
383                                 error = 10061; //WSAECONNREFUSED (Connection refused)\r
384 #if DEBUG\r
385                                 Console.WriteLine("Caught exception during Connect_internal - {0}: {1}\n{2}", e.GetType(), e.Message, e.StackTrace);\r
386 #endif\r
387                         }\r
388                 }\r
389 \r
390                 public void Listen_internal(int backlog, out int error)\r
391                 {\r
392                         error = 0;\r
393 \r
394                         if (jSocket == null || !jSocket.isBound())\r
395                         {\r
396                                 error = 10022; //WSAEINVAL (Invalid argument)\r
397                                 return;\r
398                         }\r
399 \r
400                         if (jSocket.isConnected() || jSocketChannel.isConnectionPending())\r
401                         {\r
402                                 error = 10056; //WSAEISCONN (Socket is already connected)\r
403                                 return;\r
404                         }\r
405 \r
406                         bool blockMode = jSocketChannel.isBlocking();\r
407                         bool reuseAddr = jSocket.getReuseAddress();\r
408 \r
409                         try\r
410                         {\r
411                                 jSocket.close();\r
412                         }\r
413                         catch (Exception e)\r
414                         {\r
415 #if DEBUG\r
416                                 Console.WriteLine("Caught exception during Listen_internal close old jSocket - {0}: {1}\n{2}", e.GetType(), e.Message, e.StackTrace);\r
417 #endif\r
418                         }\r
419 \r
420                         try\r
421                         {\r
422                                 jSocketChannel.close();\r
423                         }\r
424                         catch (Exception e)\r
425                         {\r
426 #if DEBUG\r
427                                 Console.WriteLine("Caught exception during Listen_internal close old jSocketChannel - {0}: {1}\n{2}", e.GetType(), e.Message, e.StackTrace);\r
428 #endif\r
429                         }\r
430 \r
431                         jSocket = null;\r
432                         jSocketChannel = null;\r
433 \r
434                         try\r
435                         {\r
436                                 jServerSocketChannel = java.nio.channels.ServerSocketChannel.open();\r
437                                 jServerSocket = jServerSocketChannel.socket();\r
438                                 jServerSocket.bind(jTempLocalSocketAddress, backlog);\r
439                                 jServerSocketChannel.configureBlocking(blockMode);\r
440                                 jServerSocket.setReuseAddress(reuseAddr);\r
441                         }\r
442                         catch (Exception e)\r
443                         {\r
444                                 error = 10048; //WSAEADDRINUSE (Address already in use)\r
445 #if DEBUG\r
446                                 Console.WriteLine("Caught exception during Listen_internal create server socket - {0}: {1}\n{2}", e.GetType(), e.Message, e.StackTrace);\r
447 #endif\r
448                         }\r
449                 }\r
450 \r
451                 public bool Poll_internal (SelectMode mode, int timeout, Socket source, out int error)\r
452                 {\r
453                         error = 0;\r
454 \r
455                         if (mode == SelectMode.SelectError && !jSocketChannel.isConnectionPending())\r
456                         {\r
457                                 return false;\r
458                         }\r
459 \r
460                         java.nio.channels.Selector selector = java.nio.channels.Selector.open();\r
461                         RegisterSelector(selector, ((mode == SelectMode.SelectRead)?0:1), source, out error);\r
462 \r
463                         if (error != 0)\r
464                         {\r
465                                 error = 0;\r
466                                 GHSocketFactory.CloseSelector(selector);\r
467                                 return (mode == SelectMode.SelectError);\r
468                         }\r
469 \r
470                         bool retVal = false;\r
471 \r
472                         long timeOutMillis = 1;\r
473                         if (timeout < 0)\r
474                         {\r
475                                 timeOutMillis = 0;\r
476                         } \r
477                         else if (timeout > 999)\r
478                         {\r
479                                 timeOutMillis = (long)(timeout / 1000);\r
480                         }\r
481 \r
482                         int readyCount = 0;\r
483                         try\r
484                         {\r
485                                 readyCount = selector.select(timeOutMillis);\r
486                         }\r
487                         catch (Exception e)\r
488                         {\r
489                                 error = 10022; //WSAEINVAL (Invalid argument)\r
490 #if DEBUG\r
491                                 Console.WriteLine("Caught exception during Poll_internal selector.select - {0}: {1}\n{2}", e.GetType(), e.Message, e.StackTrace);\r
492 #endif\r
493                         }\r
494 \r
495                         if (readyCount > 0)\r
496                         {\r
497                                 if (jSocket != null && jSocketChannel.isConnectionPending())\r
498                                 {\r
499                                         bool status = false;\r
500                                         try\r
501                                         {\r
502                                                 status = jSocketChannel.finishConnect();\r
503                                         }\r
504                                         catch (Exception e)\r
505                                         {\r
506 #if DEBUG\r
507                                                 Console.WriteLine("Caught exception during Poll_internal, finishConnect - {0}: {1}\n{2}", e.GetType(), e.Message, e.StackTrace);\r
508 #endif\r
509                                         }\r
510                                         if (status)\r
511                                         {\r
512                                                 retVal =  (mode != SelectMode.SelectError);\r
513                                         }\r
514                                         else \r
515                                         {\r
516                                                 retVal =  (mode == SelectMode.SelectError);\r
517                                         }\r
518                                 }\r
519                                 else\r
520                                 {\r
521                                         retVal =  true;\r
522                                 }\r
523                         }\r
524 \r
525                         GHSocketFactory.CloseSelector(selector);\r
526 \r
527                         return retVal;\r
528                 }\r
529 \r
530                 public void RegisterSelector(java.nio.channels.Selector selector, int mode, Socket source, out int error)\r
531                 {\r
532                         error = 0;\r
533                         if (jServerSocket != null)\r
534                         {\r
535                                 // only accept operation, which included to the read list, is allowed for server sockets\r
536                                 if (mode != 0)\r
537                                 {\r
538 //                                      error = 10038; //WSAENOTSOCK (Socket operation on nonsocket)\r
539 #if DEBUG\r
540                                         Console.WriteLine("RegisterSelector, invalid mode {0} for the server socket", mode);\r
541 #endif\r
542                                         return;\r
543                                 }\r
544 \r
545                                 try\r
546                                 {\r
547                                         if (jServerSocketChannel.isBlocking())\r
548                                         {\r
549                                                 /*\r
550                                                         A channel must be placed into non-blocking mode before being registered \r
551                                                         with a selector, and may not be returned to blocking mode until it has been \r
552                                                         deregistered. \r
553                                                 */\r
554                                                 jServerSocketChannel.configureBlocking(false);\r
555                                         }\r
556 \r
557                                         jServerSocketChannel.register(selector, java.nio.channels.SelectionKey.OP_ACCEPT, source);\r
558                                 }\r
559                                 catch (Exception e)\r
560                                 {\r
561                                         error = 10022; //WSAEINVAL (Invalid argument)\r
562 #if DEBUG\r
563                                         Console.WriteLine("Caught exception during RegisterSelector, register server socket - {0}: {1}\n{2}", e.GetType(), e.Message, e.StackTrace);\r
564 #endif\r
565                                 }\r
566                         }\r
567                         else\r
568                         {\r
569                                 try\r
570                                 {\r
571                                         int ops = java.nio.channels.SelectionKey.OP_READ;\r
572                                         if (mode > 0)\r
573                                         {\r
574                                                 if (jSocketChannel.isConnectionPending())\r
575                                                 {\r
576                                                         ops = java.nio.channels.SelectionKey.OP_CONNECT;\r
577                                                 }\r
578                                                 else\r
579                                                 {\r
580                                                         ops = java.nio.channels.SelectionKey.OP_WRITE;\r
581                                                 }\r
582                                         }\r
583                                         \r
584                                         if (jSocketChannel.isBlocking())\r
585                                         {\r
586                                                 /*\r
587                                                         A channel must be placed into non-blocking mode before being registered \r
588                                                         with a selector, and may not be returned to blocking mode until it has been \r
589                                                         deregistered. \r
590                                                 */\r
591                                                 jSocketChannel.configureBlocking(false);\r
592                                         }\r
593 \r
594                                         jSocketChannel.register(selector, ops, source);\r
595                                 }\r
596                                 catch (Exception e)\r
597                                 {\r
598                                         error = 10022; //WSAEINVAL (Invalid argument)\r
599 #if DEBUG\r
600                                         Console.WriteLine("Caught exception during RegisterSelector, register client socket - {0}: {1}\n{2}", e.GetType(), e.Message, e.StackTrace);\r
601 #endif\r
602                                 }\r
603                         }\r
604                 }\r
605 \r
606                 public bool CheckConnectionFinished()\r
607                 {\r
608                         bool status = true;\r
609                         if (jSocket != null && jSocketChannel.isConnectionPending())\r
610                         {\r
611                                 try\r
612                                 {\r
613                                         status = jSocketChannel.finishConnect();\r
614                                 }\r
615                                 catch (Exception e)\r
616                                 {\r
617                                         status = false;\r
618 #if DEBUG\r
619                                         Console.WriteLine("Caught exception during Poll_internal, finishConnect - {0}: {1}\n{2}", e.GetType(), e.Message, e.StackTrace);\r
620 #endif\r
621                                 }\r
622                         }\r
623 \r
624                         return status;\r
625                 }\r
626 \r
627                 public int Receive_internal(byte[] buffer,      int offset,     int count, SocketFlags flags,\r
628                         out int error)\r
629                 {\r
630                         error = 0;\r
631                         int ret = 0;\r
632 \r
633                         if (jSocket == null)\r
634                         {\r
635                                 error = 10057; //WSAENOTCONN (Socket is not connected)\r
636                                 return ret;\r
637                         }\r
638 \r
639                         try\r
640                         {\r
641                                 if (jSocketChannel.isConnectionPending())\r
642                                 {\r
643                                         bool status = jSocketChannel.finishConnect();\r
644                                         if (!status)\r
645                                         {\r
646                                                 error = 10022; //WSAEINVAL (Invalid argument)\r
647 #if DEBUG\r
648                                                 Console.WriteLine("Receive_internal, jSocketChannel.finishConnect return false");\r
649 #endif\r
650                                                 return 0;\r
651                                         }\r
652                                 }\r
653                                 else if (!jSocketChannel.isConnected())\r
654                                 {\r
655                                         error = 10057; //WSAENOTCONN (Socket is not connected)\r
656                                         return ret;\r
657                                 }\r
658 \r
659                                 java.nio.ByteBuffer readBuff = java.nio.ByteBuffer.wrap(vmw.common.TypeUtils.ToSByteArray(buffer), offset, count);\r
660                                 ret = jSocketChannel.read(readBuff);\r
661                                 if (ret < 0) ret = 0;\r
662                         }\r
663                         catch (Exception e)\r
664                         {\r
665                                 error = 10054; //WSAECONNRESET (Connection reset by peer)\r
666                                 ret = 0;\r
667 #if DEBUG\r
668                                 Console.WriteLine("Caught exception during Receive_internal - {0}: {1}\n{2}", e.GetType(), e.Message, e.StackTrace);\r
669 #endif\r
670                         }\r
671 \r
672                         if (ret == 0 && !jSocketChannel.isBlocking())\r
673                         {\r
674                                 error = 10035; //WSAEWOULDBLOCK (Resource temporarily unavailable)\r
675                         }\r
676                         return ret;\r
677                 }\r
678 \r
679                 public int RecvFrom_internal(byte[] buffer, int offset, int count,      SocketFlags flags,\r
680                         ref SocketAddress sockaddr, out int error)\r
681                 {\r
682                         return Receive_internal(buffer, offset, count, flags, out error);\r
683                 }\r
684 \r
685                 public int Send_internal(byte[] buf, int offset, int count, SocketFlags flags,\r
686                         out int error)\r
687                 {\r
688                         error = 0;\r
689                         int ret = 0;\r
690 \r
691                         if (jSocket == null)\r
692                         {\r
693                                 error = 10057; //WSAENOTCONN (Socket is not connected)\r
694                                 return ret;\r
695                         }\r
696 \r
697                         try\r
698                         {\r
699                                 if (jSocketChannel.isConnectionPending())\r
700                                 {\r
701                                         bool status = jSocketChannel.finishConnect();\r
702                                         if (!status)\r
703                                         {\r
704                                                 error = 10022; //WSAEINVAL (Invalid argument)\r
705 #if DEBUG\r
706                                                 Console.WriteLine("Send_internal, jSocketChannel.finishConnect return false");\r
707 #endif\r
708                                                 return 0;\r
709                                         }\r
710                                 }\r
711                                 else if (!jSocketChannel.isConnected())\r
712                                 {\r
713                                         error = 10057; //WSAENOTCONN (Socket is not connected)\r
714                                         return ret;\r
715                                 }\r
716 \r
717                                 java.nio.ByteBuffer writeBuff = java.nio.ByteBuffer.wrap(vmw.common.TypeUtils.ToSByteArray(buf), offset, count);\r
718                                 ret = jSocketChannel.write(writeBuff);\r
719                                 if (ret < 0) ret = 0;\r
720                         }\r
721                         catch (Exception e)\r
722                         {\r
723                                 error = 10054; //WSAECONNRESET (Connection reset by peer)\r
724                                 ret = 0;\r
725 #if DEBUG\r
726                                 Console.WriteLine("Caught exception during Send_internal - {0}: {1}\n{2}", e.GetType(), e.Message, e.StackTrace);\r
727 #endif\r
728                         }\r
729 \r
730                         if (ret == 0 && !jSocketChannel.isBlocking())\r
731                         {\r
732                                 error = 10035; //WSAEWOULDBLOCK (Resource temporarily unavailable)\r
733                         }\r
734                         return ret;\r
735                 }\r
736 \r
737                 public int SendTo_internal(byte[] buffer, int offset, int count,\r
738                         SocketFlags flags,      SocketAddress sa, out int error)\r
739                 {\r
740                         return Send_internal(buffer, offset, count, flags, out error);\r
741                 }\r
742 \r
743                 public void SetSocketOption_internal (SocketOptionLevel level,\r
744                         SocketOptionName name, object obj_val,\r
745                         byte [] byte_val, int int_val, out int error)\r
746                 {\r
747                         error = 0;\r
748 \r
749                         if (byte_val != null)\r
750                         {\r
751                                 error = -1;\r
752                                 throw new NotImplementedException();\r
753                         }\r
754 \r
755                         if (jSocket == null && jServerSocket == null)\r
756                         {\r
757                                 error = 10022; //WSAEINVAL (Invalid argument)\r
758                                 return;\r
759                         }\r
760 \r
761                         switch (level)\r
762                         {\r
763                                 case SocketOptionLevel.IPv6:\r
764                                         error = 10042; //WSAENOPROTOOPT (Bad protocol option)\r
765                                         return;\r
766                                 case SocketOptionLevel.IP:\r
767                                         if (name != SocketOptionName.NoDelay)\r
768                                         {\r
769                                                 error = 10042; //WSAENOPROTOOPT (Bad protocol option)\r
770                                                 return;\r
771                                         }\r
772                                         break;\r
773                                 case SocketOptionLevel.Udp:\r
774                                         if (name == SocketOptionName.NoDelay)\r
775                                         {\r
776                                                 error = 10042; //WSAENOPROTOOPT (Bad protocol option)\r
777                                         }\r
778                                         else\r
779                                         {\r
780                                                 error = 10022; //WSAEINVAL (Invalid argument)\r
781                                         }\r
782                                         return;\r
783                                 case SocketOptionLevel.Tcp:\r
784                                         if (name != SocketOptionName.NoDelay)\r
785                                         {\r
786                                                 error = 10022; //WSAEINVAL (Invalid argument)\r
787                                                 return;\r
788                                         }\r
789                                         break;\r
790                         }\r
791 \r
792                         try\r
793                         {\r
794                                 bool bval = false;\r
795                                 int ival = 0;\r
796                                 switch (name)\r
797                                 {\r
798                                         case SocketOptionName.DontLinger:\r
799                                                 jSocket.setSoLinger(false, 0);\r
800                                                 break;\r
801                                         case SocketOptionName.Linger:\r
802                                                 LingerOption lval = obj_val as LingerOption;\r
803                                                 if (lval != null)\r
804                                                 {\r
805                                                         jSocket.setSoLinger(lval.Enabled, lval.LingerTime);\r
806                                                 }\r
807                                                 else\r
808                                                 {\r
809                                                         error = 10022; //WSAEINVAL (Invalid argument)\r
810                                                 }\r
811                                                 break;\r
812                                         case SocketOptionName.KeepAlive:\r
813                                                 if (obj_val != null)\r
814                                                 {\r
815                                                         bval = ((int)obj_val == 0)?false:true;\r
816                                                 }\r
817                                                 else\r
818                                                 {\r
819                                                         bval = (int_val == 0)?false:true;\r
820                                                 }\r
821                                                 jSocket.setKeepAlive(bval);\r
822                                                 break;\r
823                                         case SocketOptionName.NoDelay:\r
824                                                 if (obj_val != null)\r
825                                                 {\r
826                                                         bval = ((int)obj_val == 0)?false:true;\r
827                                                 }\r
828                                                 else\r
829                                                 {\r
830                                                         bval = (int_val == 0)?false:true;\r
831                                                 }\r
832                                                 jSocket.setTcpNoDelay(bval);\r
833                                                 break;\r
834                                         case SocketOptionName.ReceiveBuffer:\r
835                                                 ival = int_val;\r
836                                                 if (obj_val != null)\r
837                                                 {\r
838                                                         ival = (int) obj_val;\r
839                                                 }\r
840                                                 if (jServerSocket != null)\r
841                                                 {\r
842                                                         jServerSocket.setReceiveBufferSize(ival);\r
843                                                 }\r
844                                                 else\r
845                                                 {\r
846                                                         jSocket.setReceiveBufferSize(ival);\r
847                                                 }\r
848                                                 break;\r
849                                         case SocketOptionName.ReceiveTimeout:\r
850                                                 ival = int_val;\r
851                                                 if (obj_val != null)\r
852                                                 {\r
853                                                         ival = (int) obj_val;\r
854                                                 }\r
855                                                 if (jServerSocket != null)\r
856                                                 {\r
857                                                         jServerSocket.setSoTimeout(ival);\r
858                                                 }\r
859                                                 else\r
860                                                 {\r
861                                                         jSocket.setSoTimeout(ival);\r
862                                                 }\r
863                                                 break;\r
864                                         case SocketOptionName.ReuseAddress:\r
865                                                 if (obj_val != null)\r
866                                                 {\r
867                                                         bval = ((int)obj_val == 0)?false:true;\r
868                                                 }\r
869                                                 else\r
870                                                 {\r
871                                                         bval = (int_val == 0)?false:true;\r
872                                                 }\r
873                                                 if (jServerSocket != null)\r
874                                                 {\r
875                                                         jServerSocket.setReuseAddress(bval);\r
876                                                 }\r
877                                                 else\r
878                                                 {\r
879                                                         jSocket.setReuseAddress(bval);\r
880                                                 }\r
881                                                 break;\r
882                                         case SocketOptionName.SendBuffer:\r
883                                                 ival = int_val;\r
884                                                 if (obj_val != null)\r
885                                                 {\r
886                                                         ival = (int) obj_val;\r
887                                                 }\r
888                                                 jSocket.setSendBufferSize(ival);\r
889                                                 break;\r
890                                         case SocketOptionName.OutOfBandInline:\r
891                                                 if (obj_val != null)\r
892                                                 {\r
893                                                         bval = ((int)obj_val == 0)?false:true;\r
894                                                 }\r
895                                                 else\r
896                                                 {\r
897                                                         bval = (int_val == 0)?false:true;\r
898                                                 }\r
899                                                 jSocket.setOOBInline(bval);\r
900                                                 break;\r
901                                         default:\r
902                                                 error = 10022; //WSAEINVAL (Invalid argument)\r
903                                                 break;\r
904                                 }\r
905                         }\r
906                         catch (Exception e)\r
907                         {\r
908                                 error = 10022; //WSAEINVAL (Invalid argument)\r
909                                 obj_val = null;\r
910                         }\r
911                 }\r
912 \r
913                 public void GetSocketOption_obj_internal(SocketOptionLevel level, SocketOptionName name, \r
914                         out object obj_val, out int error)\r
915                 {\r
916                         obj_val = null;\r
917                         error = 0;\r
918 \r
919                         if (jSocket == null && jServerSocket == null)\r
920                         {\r
921                                 error = 10022; //WSAEINVAL (Invalid argument)\r
922                                 return;\r
923                         }\r
924 \r
925                         switch (level)\r
926                         {\r
927                                 case SocketOptionLevel.IPv6:\r
928                                         error = 10042; //WSAENOPROTOOPT (Bad protocol option)\r
929                                         return;\r
930                                 case SocketOptionLevel.IP:\r
931                                         if (name != SocketOptionName.NoDelay)\r
932                                         {\r
933                                                 error = 10042; //WSAENOPROTOOPT (Bad protocol option)\r
934                                                 return;\r
935                                         }\r
936                                         break;\r
937                                 case SocketOptionLevel.Udp:\r
938                                         if (name == SocketOptionName.NoDelay)\r
939                                         {\r
940                                                 error = 10042; //WSAENOPROTOOPT (Bad protocol option)\r
941                                         }\r
942                                         else\r
943                                         {\r
944                                                 error = 10022; //WSAEINVAL (Invalid argument)\r
945                                         }\r
946                                         return;\r
947                                 case SocketOptionLevel.Tcp:\r
948                                         if (name != SocketOptionName.NoDelay)\r
949                                         {\r
950                                                 error = 10022; //WSAEINVAL (Invalid argument)\r
951                                                 return;\r
952                                         }\r
953                                         break;\r
954                         }\r
955 \r
956                         try\r
957                         {\r
958                                 bool bval = false;\r
959                                 int ival = 0;\r
960                                 switch (name)\r
961                                 {\r
962                                         case SocketOptionName.DontLinger:\r
963                                                 ival = jSocket.getSoLinger();\r
964                                                 if (ival == -1)\r
965                                                 {\r
966                                                         obj_val = 1;\r
967                                                 }\r
968                                                 else\r
969                                                 {\r
970                                                         obj_val = 0;\r
971                                                 }\r
972                                                 break;\r
973                                         case SocketOptionName.Linger:\r
974                                                 ival = jSocket.getSoLinger();\r
975                                                 if (ival == -1)\r
976                                                 {\r
977                                                         ival = 0;\r
978                                                 }\r
979                                                 LingerOption ret = new LingerOption((ival != 0), ival);\r
980                                                 obj_val = ret;\r
981                                                 break;\r
982                                         case SocketOptionName.KeepAlive:\r
983                                                 bval = jSocket.getKeepAlive();\r
984                                                 obj_val = ((bval)?1:0);\r
985                                                 break;\r
986                                         case SocketOptionName.NoDelay:\r
987                                                 bval = jSocket.getTcpNoDelay();\r
988                                                 obj_val = ((bval)?1:0);\r
989                                                 break;\r
990                                         case SocketOptionName.ReceiveBuffer:\r
991                                                 if (jServerSocket != null)\r
992                                                 {\r
993                                                         ival = jServerSocket.getReceiveBufferSize();\r
994                                                 }\r
995                                                 else\r
996                                                 {\r
997                                                         ival = jSocket.getReceiveBufferSize();\r
998                                                 }\r
999                                                 obj_val = ival;\r
1000                                                 break;\r
1001                                         case SocketOptionName.ReceiveTimeout:\r
1002                                                 if (jServerSocket != null)\r
1003                                                 {\r
1004                                                         ival = jServerSocket.getSoTimeout();\r
1005                                                 }\r
1006                                                 else\r
1007                                                 {\r
1008                                                         ival = jSocket.getSoTimeout();\r
1009                                                 }\r
1010                                                 obj_val = ival;\r
1011                                                 break;\r
1012                                         case SocketOptionName.ReuseAddress:\r
1013                                                 if (jServerSocket != null)\r
1014                                                 {\r
1015                                                         bval = jServerSocket.getReuseAddress();\r
1016                                                 }\r
1017                                                 else\r
1018                                                 {\r
1019                                                         bval = jSocket.getReuseAddress();\r
1020                                                 }\r
1021                                                 obj_val = ((bval)?1:0);\r
1022                                                 break;\r
1023                                         case SocketOptionName.SendBuffer:\r
1024                                                 ival = jSocket.getSendBufferSize();\r
1025                                                 obj_val = ival;\r
1026                                                 break;\r
1027                                         case SocketOptionName.OutOfBandInline:\r
1028                                                 bval = jSocket.getOOBInline();\r
1029                                                 obj_val = ((bval)?1:0);\r
1030                                                 break;\r
1031                                         default:\r
1032                                                 error = 10022; //WSAEINVAL (Invalid argument)\r
1033                                                 break;\r
1034                                 }\r
1035                         }\r
1036                         catch (Exception e)\r
1037                         {\r
1038                                 error = 10022; //WSAEINVAL (Invalid argument)\r
1039                                 obj_val = null;\r
1040                         }\r
1041                 }\r
1042                 \r
1043                 public void GetSocketOption_arr_internal(SocketOptionLevel level, SocketOptionName name, \r
1044                         ref byte[] byte_val, out int error)\r
1045                 {\r
1046                         error = -1;\r
1047                         throw new NotImplementedException();\r
1048                 }\r
1049 \r
1050                 public int WSAIoctl (int ioctl_code, byte [] input, byte [] output, out int error)\r
1051                 {\r
1052                         error = -1;\r
1053                         throw new NotImplementedException();\r
1054                 }\r
1055 \r
1056                 public void Shutdown_internal(SocketShutdown how, out int error)\r
1057                 {\r
1058                         error = 0;\r
1059 \r
1060                         if (jServerSocket != null || jSocket == null || !jSocket.isConnected())\r
1061                         {\r
1062                                 error = 10057; //WSAENOTCONN (Socket is not connected)\r
1063                                 return;\r
1064                         }\r
1065 \r
1066                         try\r
1067                         {\r
1068                                 switch (how)\r
1069                                 {\r
1070                                         case SocketShutdown.Receive: \r
1071                                                                 jSocket.shutdownInput();\r
1072                                                                 break;\r
1073                                         case SocketShutdown.Send: \r
1074                                                                                 jSocket.shutdownOutput();\r
1075                                                                                 break;\r
1076                                         case SocketShutdown.Both: \r
1077                                                                                 jSocket.shutdownInput();\r
1078                                                                                 jSocket.shutdownOutput();\r
1079                                                                                 break;\r
1080                                 }\r
1081                         }\r
1082                         catch (Exception e)\r
1083                         {\r
1084                                 error = 10022; //WSAEINVAL (Invalid argument)\r
1085 #if DEBUG\r
1086                                 Console.WriteLine("Caught exception during Shutdown_internal - {0}: {1}\n{2}", e.GetType(), e.Message, e.StackTrace);\r
1087 #endif\r
1088                         }\r
1089                 }\r
1090 \r
1091                 private java.io.FileInputStream searchDefaultCacerts()\r
1092                 {\r
1093                         try\r
1094                         {\r
1095                                 string javaHome = java.lang.System.getProperty("java.home");\r
1096                                 if(javaHome == null)\r
1097                                         return null;\r
1098 \r
1099                                 string keyStorePath = javaHome + "/lib/security/cacerts";\r
1100                                 //Console.WriteLine("keyStorePath = {0}", keyStorePath);\r
1101 \r
1102                                 java.io.File f = new java.io.File(keyStorePath);\r
1103                                 if(!f.exists())\r
1104                                         return null;\r
1105                                 return new java.io.FileInputStream(f);\r
1106                         }\r
1107                         catch(Exception e)\r
1108                         {\r
1109 #if DEBUG\r
1110                                 //todo log it\r
1111                                 Console.WriteLine(e.GetType() + ":" + e.Message + "\n" + e.StackTrace);\r
1112 #endif\r
1113                                 return null;\r
1114                         }\r
1115                 }\r
1116 \r
1117                 private SSLSocketFactory getSSLSocketFactory()\r
1118                 {\r
1119                         SSLSocketFactory factory = null;\r
1120 \r
1121                         try\r
1122                         {\r
1123                                 //reading the keyStore path and password from the environment properties\r
1124                                 string keyStorePath = java.lang.System.getProperty("javax.net.ssl.keyStore");\r
1125                                 java.io.FileInputStream keyStoreStream = null;\r
1126                                 if (keyStorePath != null)\r
1127                                 {\r
1128                                         java.io.File file = new java.io.File(keyStorePath);\r
1129                                         if(file.exists())\r
1130                                                 keyStoreStream = new java.io.FileInputStream(file);\r
1131                                         else\r
1132                                                 keyStoreStream = searchDefaultCacerts();\r
1133                                 }\r
1134                                 else\r
1135                                         keyStoreStream = searchDefaultCacerts();\r
1136 \r
1137                                 string keyStorePassWord = java.lang.System.getProperty("javax.net.ssl.keyStorePassword");\r
1138                                 if (keyStorePassWord == null)\r
1139                                         keyStorePassWord = "changeit";\r
1140                                 char[] passphrase = keyStorePassWord.ToCharArray();                             \r
1141                                                 \r
1142                                 //initiating SSLContext\r
1143                                 SSLContext ctx = SSLContext.getInstance("TLS");\r
1144                                 KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());\r
1145                                 TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());\r
1146                                 KeyStore ks = KeyStore.getInstance("JKS");\r
1147                                 if (keyStoreStream != null)\r
1148                                         ks.load(keyStoreStream,passphrase);\r
1149                                 else\r
1150                                         ks.load(null,null);\r
1151                                 kmf.init(ks, passphrase);\r
1152                                 tmf.init(ks);\r
1153                                 ctx.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);\r
1154 \r
1155                                 factory = ctx.getSocketFactory();\r
1156                         }\r
1157                         catch (Exception e)\r
1158                         {\r
1159                                 factory = null;\r
1160 #if DEBUG\r
1161                                 Console.WriteLine("Can't get SSL Socket Factory, the exception is {0}, {1}", e.GetType(), e.Message);\r
1162 #endif\r
1163                         }\r
1164 \r
1165                         return factory;\r
1166                 }\r
1167 \r
1168                 public GHSocket ChangeToSSL(EndPoint remote_end)\r
1169                 {\r
1170                         if (jSocket == null)\r
1171                         {\r
1172                                 throw new InvalidOperationException("The underlying socket is null");\r
1173                         }\r
1174 \r
1175                         if (!jSocketChannel.isBlocking())\r
1176                         {\r
1177                                 throw new NotImplementedException("The SSL Socket for non-blocking mode is not supported");\r
1178                         }\r
1179 \r
1180                         SSLSocketFactory factory = getSSLSocketFactory();\r
1181                         if (factory == null)\r
1182                         {\r
1183                                 throw new ApplicationException("Can't get SSL Socket Factory");\r
1184                         }\r
1185 \r
1186                         int err;\r
1187 \r
1188                         // The problem with local address, when I closed the socket and try to create the new one\r
1189                         // bounded to the given local address, I receive exception "Address already in use"\r
1190                         IPEndPoint localEndPoint = null;\r
1191 //                      IPEndPoint localEndPoint = (IPEndPoint) LocalEndPoint_internal(out err);\r
1192 //                      if (err != 0)\r
1193 //                              localEndPoint = null;\r
1194 \r
1195                         IPEndPoint remoteEndPoint = remote_end as IPEndPoint;\r
1196                         if (remoteEndPoint == null)\r
1197                         {\r
1198                                 remoteEndPoint = (IPEndPoint) RemoteEndPoint_internal(out err);\r
1199                                 if (err != 0)\r
1200                                         remoteEndPoint = null;\r
1201                         }\r
1202 \r
1203                         java.net.Socket sslSocket = null;\r
1204                         try\r
1205                         {\r
1206                                 if (remoteEndPoint != null)\r
1207                                 {\r
1208                                         if (localEndPoint != null)\r
1209                                         {\r
1210                                                 sslSocket = factory.createSocket(\r
1211                                                         java.net.InetAddress.getByName(remoteEndPoint.Address.ToString()),\r
1212                                                         remoteEndPoint.Port,\r
1213                                                         java.net.InetAddress.getByName(localEndPoint.Address.ToString()),\r
1214                                                         localEndPoint.Port);\r
1215                                         }\r
1216                                         else\r
1217                                         {\r
1218                                                 sslSocket = factory.createSocket(\r
1219                                                         jSocket, \r
1220                                                         remoteEndPoint.Address.ToString(),\r
1221                                                         remoteEndPoint.Port,\r
1222                                                         false);\r
1223                                         }\r
1224 \r
1225                                         if (sslSocket != null)\r
1226                                         {\r
1227                                                 String[] protocols = { "TLSv1", "SSLv3" };\r
1228                                                 ((SSLSocket)sslSocket).setUseClientMode(true);\r
1229                                                 ((SSLSocket)sslSocket).startHandshake();\r
1230                                         }\r
1231 \r
1232                                 }\r
1233                                 else\r
1234                                 {\r
1235                                         sslSocket = factory.createSocket();\r
1236                                 }\r
1237                         }\r
1238                         catch (Exception e)\r
1239                         {\r
1240                                 sslSocket = null;\r
1241 #if DEBUG\r
1242                                 Console.WriteLine("Can't create SSL Socket, the exception is {0}, {1}", e.GetType(), e.Message);\r
1243 #endif\r
1244                         }\r
1245 \r
1246                         if (sslSocket == null)\r
1247                         {\r
1248 //                              throw new ApplicationException("Can't create SSL Socket");\r
1249                                 // it is important to the Socket class to distinguish if the underlying \r
1250                                 // handle (GHSocket) is still valid and can be used as non-SSL, or it is already\r
1251                                 // closed by this function and can't be used any more.\r
1252                                 return null;\r
1253                         }\r
1254 \r
1255 /*\r
1256                         string[] arr = ((SSLSocket)sslSocket).getEnabledProtocols();\r
1257                         if (arr != null)\r
1258                         {\r
1259                                 foreach (string s in arr)\r
1260                                         Console.WriteLine("s:"+s);\r
1261                         }\r
1262                         string [] arr1 = ((SSLSocket)sslSocket).getEnabledCipherSuites();\r
1263                         if (arr1 != null)\r
1264                         {\r
1265                                 foreach (string s in arr1)\r
1266                                         Console.WriteLine("s:"+s);\r
1267                         }\r
1268 */\r
1269 \r
1270                         return new GHStreamSocketSSL(sslSocket);\r
1271                 }\r
1272         }\r
1273 }\r