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