@@ -65,7 +65,15 @@ void Cancel()
65
65
}
66
66
cts . Token . Register ( Cancel ) ;
67
67
68
- _socket . Connect ( _endpoint ) ;
68
+ try
69
+ {
70
+ _socket . Connect ( _endpoint ) ;
71
+ }
72
+ catch ( PlatformNotSupportedException )
73
+ {
74
+ var ep = GetIPEndPoint ( _endpoint ) ;
75
+ _socket . Connect ( ep . Address , ep . Port ) ;
76
+ }
69
77
70
78
if ( _socket != null )
71
79
{
@@ -94,20 +102,28 @@ public async Task ConnectAsync()
94
102
{
95
103
bool success = false ;
96
104
97
- var connTask = _socket . ConnectAsync ( _endpoint ) ;
98
-
99
- if ( await Task . WhenAny ( connTask , Task . Delay ( _connectionTimeout ) ) == connTask )
100
- {
101
- await connTask ;
102
- }
103
- else
105
+ try
104
106
{
105
- if ( _socket != null )
107
+ var connTask = _socket . ConnectAsync ( _endpoint ) ;
108
+
109
+ if ( await Task . WhenAny ( connTask , Task . Delay ( _connectionTimeout ) ) == connTask )
106
110
{
107
- _socket . Dispose ( ) ;
108
- _socket = null ;
111
+ await connTask ;
109
112
}
110
- throw new TimeoutException ( $ "Timeout to connect to { _endpoint } .") ;
113
+ else
114
+ {
115
+ if ( _socket != null )
116
+ {
117
+ _socket . Dispose ( ) ;
118
+ _socket = null ;
119
+ }
120
+ throw new TimeoutException ( $ "Timeout to connect to { _endpoint } .") ;
121
+ }
122
+ }
123
+ catch ( PlatformNotSupportedException )
124
+ {
125
+ var ep = GetIPEndPoint ( _endpoint ) ;
126
+ await _socket . ConnectAsync ( ep . Address , ep . Port ) ;
111
127
}
112
128
113
129
if ( _socket != null )
@@ -423,6 +439,27 @@ public async Task WriteAsync(IList<ArraySegment<byte>> buffers)
423
439
throw ;
424
440
}
425
441
}
442
+
443
+ private IPEndPoint GetIPEndPoint ( EndPoint endpoint )
444
+ {
445
+ if ( endpoint is DnsEndPoint )
446
+ {
447
+ var dnsEndPoint = ( DnsEndPoint ) endpoint ;
448
+ var address = Dns . GetHostAddresses ( dnsEndPoint . Host ) . FirstOrDefault ( ip =>
449
+ ip . AddressFamily == AddressFamily . InterNetwork ) ;
450
+ if ( address == null )
451
+ throw new ArgumentException ( String . Format ( "Could not resolve host '{0}'." , endpoint ) ) ;
452
+ return new IPEndPoint ( address , dnsEndPoint . Port ) ;
453
+ }
454
+ else if ( endpoint is IPEndPoint )
455
+ {
456
+ return endpoint as IPEndPoint ;
457
+ }
458
+ else
459
+ {
460
+ throw new Exception ( "Not supported EndPoint type" ) ;
461
+ }
462
+ }
426
463
}
427
464
}
428
465
0 commit comments