diff --git a/Source/MQTTnet/Implementations/CrossPlatformSocket.cs b/Source/MQTTnet/Implementations/CrossPlatformSocket.cs index 202be6059..619760503 100644 --- a/Source/MQTTnet/Implementations/CrossPlatformSocket.cs +++ b/Source/MQTTnet/Implementations/CrossPlatformSocket.cs @@ -202,9 +202,28 @@ public async Task ConnectAsync(EndPoint endPoint, CancellationToken cancellation using (cancellationToken.Register(_socketDisposeAction)) { #if NET452 || NET461 - await Task.Factory.FromAsync(_socket.BeginConnect, _socket.EndConnect, endPoint, null).ConfigureAwait(false); + // This is a fix for Mono which behaves differently than dotnet. + // The connection will not be established when the DNS endpoint is used. + if (endPoint is DnsEndPoint dns && dns.AddressFamily == AddressFamily.Unspecified) + { + await Task.Factory.FromAsync(_socket.BeginConnect, _socket.EndConnect, dns.Host, dns.Port, null).ConfigureAwait(false); + } + else + { + await Task.Factory.FromAsync(_socket.BeginConnect, _socket.EndConnect, endPoint, null).ConfigureAwait(false); + } #else - await _socket.ConnectAsync(endPoint).ConfigureAwait(false); + + // This is a fix for Mono which behaves differently than dotnet. + // The connection will not be established when the DNS endpoint is used. + if (endPoint is DnsEndPoint dns && dns.AddressFamily == AddressFamily.Unspecified) + { + await _socket.ConnectAsync(dns.Host, dns.Port).ConfigureAwait(false); + } + else + { + await _socket.ConnectAsync(endPoint).ConfigureAwait(false); + } #endif } #endif