diff --git a/kmqtt-client/src/commonMain/kotlin/ClientSocket.kt b/kmqtt-client/src/commonMain/kotlin/ClientSocket.kt
index 52e9070820f4ae613f137dbbb86d425317780a31..46d4841b33f85f5cd0567e51409d1e73567e55c7 100644
--- a/kmqtt-client/src/commonMain/kotlin/ClientSocket.kt
+++ b/kmqtt-client/src/commonMain/kotlin/ClientSocket.kt
@@ -5,5 +5,6 @@ public expect class ClientSocket(
     port: Int,
     maximumPacketSize: Int,
     readTimeOut: Int,
+    connectTimeOut: Int,
     checkCallback: () -> Unit
 ) : Socket
diff --git a/kmqtt-client/src/commonMain/kotlin/MQTTClient.kt b/kmqtt-client/src/commonMain/kotlin/MQTTClient.kt
index d71297a39c4e11bb2c9d0703943a7d23798ce444..934ada0966b077685e42472c48e3e2ff590a8317 100644
--- a/kmqtt-client/src/commonMain/kotlin/MQTTClient.kt
+++ b/kmqtt-client/src/commonMain/kotlin/MQTTClient.kt
@@ -42,6 +42,7 @@ import socket.tls.TLSClientSettings
  * @param willRetain set if the will PUBLISH must be retained by the server
  * @param willQos the QoS of the will PUBLISH message
  * @param connackTimeout timeout in seconds after which the connection is closed if no CONNACK packet has been received
+ * @param connectTimeout timeout in seconds after which an exception will be thrown if the socket is not able to establish a connection
  * @param enhancedAuthCallback the callback called when authenticationData is received, it should return the data necessary to continue authentication or null if completed (used only in MQTT5 if authenticationMethod has been set in the CONNECT properties)
  * @param onConnected called when the CONNACK packet has been received and the connection has been established
  * @param onDisconnected called when a DISCONNECT packet has been received or if the connection has been terminated
@@ -67,6 +68,7 @@ public class MQTTClient(
     private val willRetain: Boolean = false,
     private val willQos: Qos = Qos.AT_MOST_ONCE,
     private val connackTimeout: Int = 30,
+    private val connectTimeout: Int = 30,
     private val enhancedAuthCallback: (authenticationData: UByteArray?) -> UByteArray? = { null },
     private val onConnected: (connack: MQTTConnack) -> Unit = {},
     private val onDisconnected: (disconnect: MQTTDisconnect?) -> Unit = {},
@@ -124,16 +126,16 @@ public class MQTTClient(
 
         running.getAndSet(true)
 
-        connectSocket(250)
+        connectSocket(250, connectTimeout * 1000)
     }
 
-    private fun connectSocket(readTimeout: Int) {
+    private fun connectSocket(readTimeout: Int, connectTimeout: Int) {
         if (socket == null) {
             connackReceived.getAndSet(false)
             socket = if (tls == null)
-                ClientSocket(address, port, maximumPacketSize, readTimeout, ::check)
+                ClientSocket(address, port, maximumPacketSize, readTimeout, connectTimeout, ::check)
             else
-                TLSClientSocket(address, port, maximumPacketSize, readTimeout, tls, ::check)
+                TLSClientSocket(address, port, maximumPacketSize, readTimeout, connectTimeout, tls, ::check)
             if (webSocket != null) {
                 socket = WebSocket(socket!!, address, webSocket)
             }
diff --git a/kmqtt-client/src/commonMain/kotlin/TLSClientSocket.kt b/kmqtt-client/src/commonMain/kotlin/TLSClientSocket.kt
index 931750e5af597cf9ce51e12f0fdd8b43a5219e25..1964002269876138b74f55d4a6f7b0e0cfabde22 100644
--- a/kmqtt-client/src/commonMain/kotlin/TLSClientSocket.kt
+++ b/kmqtt-client/src/commonMain/kotlin/TLSClientSocket.kt
@@ -6,6 +6,7 @@ public expect class TLSClientSocket(
     port: Int,
     maximumPacketSize: Int,
     readTimeOut: Int,
+    connectTimeOut: Int,
     tlsSettings: TLSClientSettings,
     checkCallback: () -> Unit
 ) : TLSSocket {
diff --git a/kmqtt-client/src/jsMain/kotlin/ClientSocket.kt b/kmqtt-client/src/jsMain/kotlin/ClientSocket.kt
index 297edf6c9cf30e2802dd4ecd661927c4a5aa7035..86c85219f6875fc486eea0a0d21d3266e232b6ba 100644
--- a/kmqtt-client/src/jsMain/kotlin/ClientSocket.kt
+++ b/kmqtt-client/src/jsMain/kotlin/ClientSocket.kt
@@ -1,3 +1,4 @@
+import socket.IOException
 import socket.tcp.Socket
 import web.timers.setTimeout
 
@@ -6,6 +7,7 @@ public actual class ClientSocket actual constructor(
     port: Int,
     maximumPacketSize: Int,
     private val readTimeOut: Int,
+    private val connectTimeOut: Int,
     private val checkCallback: () -> Unit
 ) : Socket(node.net.Socket(), { _, _ ->
     checkCallback()
@@ -16,6 +18,12 @@ public actual class ClientSocket actual constructor(
 
     init {
         socket.connect(port, address)
+        setTimeout({
+            if (socket.connecting) {
+                close()
+                throw IOException("Socket connect timeout set failed")
+            }
+        }, connectTimeOut)
         doLater()
     }
 
diff --git a/kmqtt-client/src/jsMain/kotlin/TLSClientSocket.kt b/kmqtt-client/src/jsMain/kotlin/TLSClientSocket.kt
index 6e0d43b6c29fd64bc5cfc3c157d3d59254a8898d..a72f4916a50a220c63b31fd478bf9d38bf6d4b2a 100644
--- a/kmqtt-client/src/jsMain/kotlin/TLSClientSocket.kt
+++ b/kmqtt-client/src/jsMain/kotlin/TLSClientSocket.kt
@@ -13,6 +13,7 @@ public actual class TLSClientSocket actual constructor(
     port: Int,
     maximumPacketSize: Int,
     private val readTimeOut: Int,
+    connectTimeOut: Int,
     tlsSettings: TLSClientSettings,
     private val checkCallback: () -> Unit
 ) : TLSSocket(connect(port, address, TlsConnectionOptions().apply {
@@ -59,6 +60,12 @@ public actual class TLSClientSocket actual constructor(
     private var open = true
 
     init {
+        setTimeout({
+            if (socket.connecting) {
+                close()
+                throw IOException("Socket connect timeout set failed")
+            }
+        }, connectTimeOut)
         doLater()
     }
 
diff --git a/kmqtt-client/src/jvmMain/kotlin/ClientSocket.kt b/kmqtt-client/src/jvmMain/kotlin/ClientSocket.kt
index 763efb09ad416299e3600be5b53724e3aaaa01ba..6aaf823edc9e99ac30c7c56dcd8f04b816938315 100644
--- a/kmqtt-client/src/jvmMain/kotlin/ClientSocket.kt
+++ b/kmqtt-client/src/jvmMain/kotlin/ClientSocket.kt
@@ -10,9 +10,10 @@ public actual class ClientSocket actual constructor(
     port: Int,
     maximumPacketSize: Int,
     private val readTimeOut: Int,
+    connectTimeOut: Int,
     checkCallback: () -> Unit
 ) : Socket(
-    SocketChannel.open(InetSocketAddress(address, port)),
+    SocketChannel.open(),
     null,
     ByteBuffer.allocate(maximumPacketSize),
     ByteBuffer.allocate(maximumPacketSize)
@@ -21,8 +22,13 @@ public actual class ClientSocket actual constructor(
     private val selector = Selector.open()
 
     init {
+        channel.socket().connect(InetSocketAddress(address, port), connectTimeOut)
         channel.configureBlocking(false)
         channel.register(selector, SelectionKey.OP_READ)
+
+        if (!channel.isConnected) {
+            throw Exception("Connect timeout expired")
+        }
     }
 
     override fun read(): UByteArray? {
diff --git a/kmqtt-client/src/jvmMain/kotlin/TLSClientSocket.kt b/kmqtt-client/src/jvmMain/kotlin/TLSClientSocket.kt
index b6af8271da4bf1bab131f83087e036bbf259f887..d01d03d63cac1e2c2bf76fe6fb11a301c4636f74 100644
--- a/kmqtt-client/src/jvmMain/kotlin/TLSClientSocket.kt
+++ b/kmqtt-client/src/jvmMain/kotlin/TLSClientSocket.kt
@@ -25,10 +25,12 @@ public actual class TLSClientSocket actual constructor(
     port: Int,
     maximumPacketSize: Int,
     private val readTimeOut: Int,
+    private val connectTimeOut: Int,
     private val tlsSettings: TLSClientSettings,
     checkCallback: () -> Unit
 ) : TLSSocket(
-    SocketChannel.open(InetSocketAddress(address, port)).apply {
+    SocketChannel.open().apply {
+        socket().connect(InetSocketAddress(address, port), connectTimeOut)
         configureBlocking(false)
     },
     null,
diff --git a/kmqtt-client/src/posixMain/kotlin/ClientSocket.kt b/kmqtt-client/src/posixMain/kotlin/ClientSocket.kt
index 293c3a66e79906aef4181b8b9bf895c8cf8288e8..6fcf934b3409db3adfb5521017b7894c1ab8d036 100644
--- a/kmqtt-client/src/posixMain/kotlin/ClientSocket.kt
+++ b/kmqtt-client/src/posixMain/kotlin/ClientSocket.kt
@@ -8,6 +8,7 @@ public actual class ClientSocket actual constructor(
     port: Int,
     maximumPacketSize: Int,
     private val readTimeOut: Int,
+    private val connectTimeOut: Int,
     checkCallback: () -> Unit
 ) : Socket(
     socketsInit().run {
@@ -23,7 +24,13 @@ public actual class ClientSocket actual constructor(
         memScoped {
             val ip = getaddrinfo(address, port.toString()) ?: throw IOException("Failed resolving address")
 
+            if (set_send_socket_timeout(socket, connectTimeOut.convert()) == -1) {
+                socketsCleanup()
+                throw IOException("Socket connect timeout set failed, error ${getErrno()}")
+            }
+
             if (connect(socket, ip, sizeOf<sockaddr_in>().convert()) == -1) {
+                socketsCleanup()
                 throw IOException("Socket connect failed, error ${getErrno()}")
             }
 
diff --git a/kmqtt-client/src/posixMain/kotlin/TLSClientSocket.kt b/kmqtt-client/src/posixMain/kotlin/TLSClientSocket.kt
index 8b2f6cf568f330fe7f20d5537d7853ae8fc19700..bb1629b0a77abcfbdbccc5f3c9e5e464f83b34d3 100644
--- a/kmqtt-client/src/posixMain/kotlin/TLSClientSocket.kt
+++ b/kmqtt-client/src/posixMain/kotlin/TLSClientSocket.kt
@@ -9,6 +9,7 @@ public actual class TLSClientSocket actual constructor(
     port: Int,
     maximumPacketSize: Int,
     private val readTimeOut: Int,
+    private val connectTimeOut: Int,
     tlsSettings: TLSClientSettings,
     checkCallback: () -> Unit
 ) : TLSSocket(
@@ -26,7 +27,13 @@ public actual class TLSClientSocket actual constructor(
         memScoped {
             val ip = getaddrinfo(address, port.toString()) ?: throw IOException("Failed resolving address")
 
+            if (set_send_socket_timeout(socket, connectTimeOut.convert()) == -1) {
+                socketsCleanup()
+                throw IOException("Socket connect timeout set failed, error ${getErrno()}")
+            }
+
             if (connect(socket, ip, sizeOf<sockaddr_in>().convert()) == -1) {
+                socketsCleanup()
                 throw IOException("Socket connect failed, error ${getErrno()}")
             }
 
diff --git a/kmqtt-common/src/iosArm64Main/kotlin/Posix.kt b/kmqtt-common/src/iosArm64Main/kotlin/Posix.kt
index 10bb9e902ee49c13ad0176278acc2996c30bb2e9..ed515404b3948d6acb505b30e3c0ac6e8e0c28ff 100644
--- a/kmqtt-common/src/iosArm64Main/kotlin/Posix.kt
+++ b/kmqtt-common/src/iosArm64Main/kotlin/Posix.kt
@@ -127,11 +127,19 @@ public actual fun getEagain(): Int = EAGAIN
 
 public actual fun getEwouldblock(): Int = EWOULDBLOCK
 
-public actual fun MemScope.set_socket_timeout(__fd: Int, timeout: Long): Int {
+public actual fun MemScope.set_send_socket_timeout(__fd: Int, timeout: Long): Int {
     val timeoutStruct = alloc<timeval>()
     val seconds = timeout / 1000
-    timeoutStruct.tv_sec = seconds
-    timeoutStruct.tv_usec = (timeout.toInt() - seconds.toInt() * 1000) * 1000
+    timeoutStruct.tv_sec = seconds.convert()
+    timeoutStruct.tv_usec = ((timeout - seconds * 1000) * 1000).convert()
+    return setsockopt(__fd, SOL_SOCKET, SO_SNDTIMEO, timeoutStruct.ptr, sizeOf<timeval>().toUInt())
+}
+
+public actual fun MemScope.set_recv_socket_timeout(__fd: Int, timeout: Long): Int {
+    val timeoutStruct = alloc<timeval>()
+    val seconds = timeout / 1000
+    timeoutStruct.tv_sec = seconds.convert()
+    timeoutStruct.tv_usec = ((timeout - seconds * 1000) * 1000).convert()
     return setsockopt(__fd, SOL_SOCKET, SO_RCVTIMEO, timeoutStruct.ptr, sizeOf<timeval>().toUInt())
 }
 
diff --git a/kmqtt-common/src/iosSimulatorArm64Main/kotlin/Posix.kt b/kmqtt-common/src/iosSimulatorArm64Main/kotlin/Posix.kt
index 10bb9e902ee49c13ad0176278acc2996c30bb2e9..ed515404b3948d6acb505b30e3c0ac6e8e0c28ff 100644
--- a/kmqtt-common/src/iosSimulatorArm64Main/kotlin/Posix.kt
+++ b/kmqtt-common/src/iosSimulatorArm64Main/kotlin/Posix.kt
@@ -127,11 +127,19 @@ public actual fun getEagain(): Int = EAGAIN
 
 public actual fun getEwouldblock(): Int = EWOULDBLOCK
 
-public actual fun MemScope.set_socket_timeout(__fd: Int, timeout: Long): Int {
+public actual fun MemScope.set_send_socket_timeout(__fd: Int, timeout: Long): Int {
     val timeoutStruct = alloc<timeval>()
     val seconds = timeout / 1000
-    timeoutStruct.tv_sec = seconds
-    timeoutStruct.tv_usec = (timeout.toInt() - seconds.toInt() * 1000) * 1000
+    timeoutStruct.tv_sec = seconds.convert()
+    timeoutStruct.tv_usec = ((timeout - seconds * 1000) * 1000).convert()
+    return setsockopt(__fd, SOL_SOCKET, SO_SNDTIMEO, timeoutStruct.ptr, sizeOf<timeval>().toUInt())
+}
+
+public actual fun MemScope.set_recv_socket_timeout(__fd: Int, timeout: Long): Int {
+    val timeoutStruct = alloc<timeval>()
+    val seconds = timeout / 1000
+    timeoutStruct.tv_sec = seconds.convert()
+    timeoutStruct.tv_usec = ((timeout - seconds * 1000) * 1000).convert()
     return setsockopt(__fd, SOL_SOCKET, SO_RCVTIMEO, timeoutStruct.ptr, sizeOf<timeval>().toUInt())
 }
 
diff --git a/kmqtt-common/src/iosX64Main/kotlin/Posix.kt b/kmqtt-common/src/iosX64Main/kotlin/Posix.kt
index 10bb9e902ee49c13ad0176278acc2996c30bb2e9..ed515404b3948d6acb505b30e3c0ac6e8e0c28ff 100644
--- a/kmqtt-common/src/iosX64Main/kotlin/Posix.kt
+++ b/kmqtt-common/src/iosX64Main/kotlin/Posix.kt
@@ -127,11 +127,19 @@ public actual fun getEagain(): Int = EAGAIN
 
 public actual fun getEwouldblock(): Int = EWOULDBLOCK
 
-public actual fun MemScope.set_socket_timeout(__fd: Int, timeout: Long): Int {
+public actual fun MemScope.set_send_socket_timeout(__fd: Int, timeout: Long): Int {
     val timeoutStruct = alloc<timeval>()
     val seconds = timeout / 1000
-    timeoutStruct.tv_sec = seconds
-    timeoutStruct.tv_usec = (timeout.toInt() - seconds.toInt() * 1000) * 1000
+    timeoutStruct.tv_sec = seconds.convert()
+    timeoutStruct.tv_usec = ((timeout - seconds * 1000) * 1000).convert()
+    return setsockopt(__fd, SOL_SOCKET, SO_SNDTIMEO, timeoutStruct.ptr, sizeOf<timeval>().toUInt())
+}
+
+public actual fun MemScope.set_recv_socket_timeout(__fd: Int, timeout: Long): Int {
+    val timeoutStruct = alloc<timeval>()
+    val seconds = timeout / 1000
+    timeoutStruct.tv_sec = seconds.convert()
+    timeoutStruct.tv_usec = ((timeout - seconds * 1000) * 1000).convert()
     return setsockopt(__fd, SOL_SOCKET, SO_RCVTIMEO, timeoutStruct.ptr, sizeOf<timeval>().toUInt())
 }
 
diff --git a/kmqtt-common/src/linuxArm64Main/kotlin/Posix.kt b/kmqtt-common/src/linuxArm64Main/kotlin/Posix.kt
index 4ff07f579091fbb78b17ff08ad87ea6a62eaca48..9a1d5f1511273b1603e35da417966f5d4a6d03f5 100644
--- a/kmqtt-common/src/linuxArm64Main/kotlin/Posix.kt
+++ b/kmqtt-common/src/linuxArm64Main/kotlin/Posix.kt
@@ -119,11 +119,19 @@ public actual fun getEagain(): Int = EAGAIN
 
 public actual fun getEwouldblock(): Int = EWOULDBLOCK
 
-public actual fun MemScope.set_socket_timeout(__fd: Int, timeout: Long): Int {
+public actual fun MemScope.set_send_socket_timeout(__fd: Int, timeout: Long): Int {
     val timeoutStruct = alloc<timeval>()
     val seconds = timeout / 1000
-    timeoutStruct.tv_sec = seconds
-    timeoutStruct.tv_usec = (timeout - seconds * 1000) * 1000
+    timeoutStruct.tv_sec = seconds.convert()
+    timeoutStruct.tv_usec = ((timeout - seconds * 1000) * 1000).convert()
+    return setsockopt(__fd, SOL_SOCKET, SO_SNDTIMEO, timeoutStruct.ptr, sizeOf<timeval>().toUInt())
+}
+
+public actual fun MemScope.set_recv_socket_timeout(__fd: Int, timeout: Long): Int {
+    val timeoutStruct = alloc<timeval>()
+    val seconds = timeout / 1000
+    timeoutStruct.tv_sec = seconds.convert()
+    timeoutStruct.tv_usec = ((timeout - seconds * 1000) * 1000).convert()
     return setsockopt(__fd, SOL_SOCKET, SO_RCVTIMEO, timeoutStruct.ptr, sizeOf<timeval>().toUInt())
 }
 
diff --git a/kmqtt-common/src/linuxX64Main/kotlin/Posix.kt b/kmqtt-common/src/linuxX64Main/kotlin/Posix.kt
index 9e530d3bb53c41fd6e1ebdcd1c7c207f45c33f81..1afd4dfb8357c318e60dd3308515e89695b63a3f 100644
--- a/kmqtt-common/src/linuxX64Main/kotlin/Posix.kt
+++ b/kmqtt-common/src/linuxX64Main/kotlin/Posix.kt
@@ -119,11 +119,19 @@ public actual fun getEagain(): Int = EAGAIN
 
 public actual fun getEwouldblock(): Int = EWOULDBLOCK
 
-public actual fun MemScope.set_socket_timeout(__fd: Int, timeout: Long): Int {
+public actual fun MemScope.set_send_socket_timeout(__fd: Int, timeout: Long): Int {
     val timeoutStruct = alloc<timeval>()
     val seconds = timeout / 1000
-    timeoutStruct.tv_sec = seconds
-    timeoutStruct.tv_usec = (timeout - seconds * 1000) * 1000
+    timeoutStruct.tv_sec = seconds.convert()
+    timeoutStruct.tv_usec = ((timeout - seconds * 1000) * 1000).convert()
+    return setsockopt(__fd, SOL_SOCKET, SO_SNDTIMEO, timeoutStruct.ptr, sizeOf<timeval>().toUInt())
+}
+
+public actual fun MemScope.set_recv_socket_timeout(__fd: Int, timeout: Long): Int {
+    val timeoutStruct = alloc<timeval>()
+    val seconds = timeout / 1000
+    timeoutStruct.tv_sec = seconds.convert()
+    timeoutStruct.tv_usec = ((timeout - seconds * 1000) * 1000).convert()
     return setsockopt(__fd, SOL_SOCKET, SO_RCVTIMEO, timeoutStruct.ptr, sizeOf<timeval>().toUInt())
 }
 
diff --git a/kmqtt-common/src/macosArm64Main/kotlin/Posix.kt b/kmqtt-common/src/macosArm64Main/kotlin/Posix.kt
index 10bb9e902ee49c13ad0176278acc2996c30bb2e9..ed515404b3948d6acb505b30e3c0ac6e8e0c28ff 100644
--- a/kmqtt-common/src/macosArm64Main/kotlin/Posix.kt
+++ b/kmqtt-common/src/macosArm64Main/kotlin/Posix.kt
@@ -127,11 +127,19 @@ public actual fun getEagain(): Int = EAGAIN
 
 public actual fun getEwouldblock(): Int = EWOULDBLOCK
 
-public actual fun MemScope.set_socket_timeout(__fd: Int, timeout: Long): Int {
+public actual fun MemScope.set_send_socket_timeout(__fd: Int, timeout: Long): Int {
     val timeoutStruct = alloc<timeval>()
     val seconds = timeout / 1000
-    timeoutStruct.tv_sec = seconds
-    timeoutStruct.tv_usec = (timeout.toInt() - seconds.toInt() * 1000) * 1000
+    timeoutStruct.tv_sec = seconds.convert()
+    timeoutStruct.tv_usec = ((timeout - seconds * 1000) * 1000).convert()
+    return setsockopt(__fd, SOL_SOCKET, SO_SNDTIMEO, timeoutStruct.ptr, sizeOf<timeval>().toUInt())
+}
+
+public actual fun MemScope.set_recv_socket_timeout(__fd: Int, timeout: Long): Int {
+    val timeoutStruct = alloc<timeval>()
+    val seconds = timeout / 1000
+    timeoutStruct.tv_sec = seconds.convert()
+    timeoutStruct.tv_usec = ((timeout - seconds * 1000) * 1000).convert()
     return setsockopt(__fd, SOL_SOCKET, SO_RCVTIMEO, timeoutStruct.ptr, sizeOf<timeval>().toUInt())
 }
 
diff --git a/kmqtt-common/src/macosX64Main/kotlin/Posix.kt b/kmqtt-common/src/macosX64Main/kotlin/Posix.kt
index 10bb9e902ee49c13ad0176278acc2996c30bb2e9..ed515404b3948d6acb505b30e3c0ac6e8e0c28ff 100644
--- a/kmqtt-common/src/macosX64Main/kotlin/Posix.kt
+++ b/kmqtt-common/src/macosX64Main/kotlin/Posix.kt
@@ -127,11 +127,19 @@ public actual fun getEagain(): Int = EAGAIN
 
 public actual fun getEwouldblock(): Int = EWOULDBLOCK
 
-public actual fun MemScope.set_socket_timeout(__fd: Int, timeout: Long): Int {
+public actual fun MemScope.set_send_socket_timeout(__fd: Int, timeout: Long): Int {
     val timeoutStruct = alloc<timeval>()
     val seconds = timeout / 1000
-    timeoutStruct.tv_sec = seconds
-    timeoutStruct.tv_usec = (timeout.toInt() - seconds.toInt() * 1000) * 1000
+    timeoutStruct.tv_sec = seconds.convert()
+    timeoutStruct.tv_usec = ((timeout - seconds * 1000) * 1000).convert()
+    return setsockopt(__fd, SOL_SOCKET, SO_SNDTIMEO, timeoutStruct.ptr, sizeOf<timeval>().toUInt())
+}
+
+public actual fun MemScope.set_recv_socket_timeout(__fd: Int, timeout: Long): Int {
+    val timeoutStruct = alloc<timeval>()
+    val seconds = timeout / 1000
+    timeoutStruct.tv_sec = seconds.convert()
+    timeoutStruct.tv_usec = ((timeout - seconds * 1000) * 1000).convert()
     return setsockopt(__fd, SOL_SOCKET, SO_RCVTIMEO, timeoutStruct.ptr, sizeOf<timeval>().toUInt())
 }
 
diff --git a/kmqtt-common/src/mingwX64Main/kotlin/Posix.kt b/kmqtt-common/src/mingwX64Main/kotlin/Posix.kt
index e7bb40cbe91d93c1b84fce8657bb40f369d80060..a5b939aa2dff27c5503d96510338229cf05f55c5 100644
--- a/kmqtt-common/src/mingwX64Main/kotlin/Posix.kt
+++ b/kmqtt-common/src/mingwX64Main/kotlin/Posix.kt
@@ -147,7 +147,13 @@ public actual fun getEagain(): Int = WSAEWOULDBLOCK
 
 public actual fun getEwouldblock(): Int = WSAEWOULDBLOCK
 
-public actual fun MemScope.set_socket_timeout(__fd: Int, timeout: Long): Int {
+public actual fun MemScope.set_send_socket_timeout(__fd: Int, timeout: Long): Int {
+    val timeoutValue = alloc<uint32_tVar>()
+    timeoutValue.value = timeout.toUInt()
+    return setsockopt(__fd, SOL_SOCKET, platform.posix.SO_SNDTIMEO, timeoutValue.ptr, sizeOf<uint32_tVar>().toUInt())
+}
+
+public actual fun MemScope.set_recv_socket_timeout(__fd: Int, timeout: Long): Int {
     val timeoutValue = alloc<uint32_tVar>()
     timeoutValue.value = timeout.toUInt()
     return setsockopt(__fd, SOL_SOCKET, SO_RCVTIMEO, timeoutValue.ptr, sizeOf<uint32_tVar>().toUInt())
diff --git a/kmqtt-common/src/posixMain/kotlin/Posix.kt b/kmqtt-common/src/posixMain/kotlin/Posix.kt
index b6d59fd98d3f714e6057b4ea37ae462765ac3e8a..97e6a41a3b62f51b81bd4d6177fa0f4bb5ab77d8 100644
--- a/kmqtt-common/src/posixMain/kotlin/Posix.kt
+++ b/kmqtt-common/src/posixMain/kotlin/Posix.kt
@@ -71,7 +71,9 @@ public expect fun bind(__fd: Int, __addr: CValuesRef<sockaddr>?, __len: UInt): I
 
 public expect fun set_non_blocking(__fd: Int): Int
 
-public expect fun MemScope.set_socket_timeout(__fd: Int, timeout: Long): Int
+public expect fun MemScope.set_send_socket_timeout(__fd: Int, timeout: Long): Int
+
+public expect fun MemScope.set_recv_socket_timeout(__fd: Int, timeout: Long): Int
 
 public expect fun socket(__domain: Int, __type: Int, __protocol: Int): Int
 
diff --git a/kmqtt-common/src/tvosArm64Main/kotlin/Posix.kt b/kmqtt-common/src/tvosArm64Main/kotlin/Posix.kt
index 10bb9e902ee49c13ad0176278acc2996c30bb2e9..ed515404b3948d6acb505b30e3c0ac6e8e0c28ff 100644
--- a/kmqtt-common/src/tvosArm64Main/kotlin/Posix.kt
+++ b/kmqtt-common/src/tvosArm64Main/kotlin/Posix.kt
@@ -127,11 +127,19 @@ public actual fun getEagain(): Int = EAGAIN
 
 public actual fun getEwouldblock(): Int = EWOULDBLOCK
 
-public actual fun MemScope.set_socket_timeout(__fd: Int, timeout: Long): Int {
+public actual fun MemScope.set_send_socket_timeout(__fd: Int, timeout: Long): Int {
     val timeoutStruct = alloc<timeval>()
     val seconds = timeout / 1000
-    timeoutStruct.tv_sec = seconds
-    timeoutStruct.tv_usec = (timeout.toInt() - seconds.toInt() * 1000) * 1000
+    timeoutStruct.tv_sec = seconds.convert()
+    timeoutStruct.tv_usec = ((timeout - seconds * 1000) * 1000).convert()
+    return setsockopt(__fd, SOL_SOCKET, SO_SNDTIMEO, timeoutStruct.ptr, sizeOf<timeval>().toUInt())
+}
+
+public actual fun MemScope.set_recv_socket_timeout(__fd: Int, timeout: Long): Int {
+    val timeoutStruct = alloc<timeval>()
+    val seconds = timeout / 1000
+    timeoutStruct.tv_sec = seconds.convert()
+    timeoutStruct.tv_usec = ((timeout - seconds * 1000) * 1000).convert()
     return setsockopt(__fd, SOL_SOCKET, SO_RCVTIMEO, timeoutStruct.ptr, sizeOf<timeval>().toUInt())
 }
 
diff --git a/kmqtt-common/src/tvosSimulatorArm64Main/kotlin/Posix.kt b/kmqtt-common/src/tvosSimulatorArm64Main/kotlin/Posix.kt
index 10bb9e902ee49c13ad0176278acc2996c30bb2e9..ed515404b3948d6acb505b30e3c0ac6e8e0c28ff 100644
--- a/kmqtt-common/src/tvosSimulatorArm64Main/kotlin/Posix.kt
+++ b/kmqtt-common/src/tvosSimulatorArm64Main/kotlin/Posix.kt
@@ -127,11 +127,19 @@ public actual fun getEagain(): Int = EAGAIN
 
 public actual fun getEwouldblock(): Int = EWOULDBLOCK
 
-public actual fun MemScope.set_socket_timeout(__fd: Int, timeout: Long): Int {
+public actual fun MemScope.set_send_socket_timeout(__fd: Int, timeout: Long): Int {
     val timeoutStruct = alloc<timeval>()
     val seconds = timeout / 1000
-    timeoutStruct.tv_sec = seconds
-    timeoutStruct.tv_usec = (timeout.toInt() - seconds.toInt() * 1000) * 1000
+    timeoutStruct.tv_sec = seconds.convert()
+    timeoutStruct.tv_usec = ((timeout - seconds * 1000) * 1000).convert()
+    return setsockopt(__fd, SOL_SOCKET, SO_SNDTIMEO, timeoutStruct.ptr, sizeOf<timeval>().toUInt())
+}
+
+public actual fun MemScope.set_recv_socket_timeout(__fd: Int, timeout: Long): Int {
+    val timeoutStruct = alloc<timeval>()
+    val seconds = timeout / 1000
+    timeoutStruct.tv_sec = seconds.convert()
+    timeoutStruct.tv_usec = ((timeout - seconds * 1000) * 1000).convert()
     return setsockopt(__fd, SOL_SOCKET, SO_RCVTIMEO, timeoutStruct.ptr, sizeOf<timeval>().toUInt())
 }
 
diff --git a/kmqtt-common/src/tvosX64Main/kotlin/Posix.kt b/kmqtt-common/src/tvosX64Main/kotlin/Posix.kt
index 10bb9e902ee49c13ad0176278acc2996c30bb2e9..ed515404b3948d6acb505b30e3c0ac6e8e0c28ff 100644
--- a/kmqtt-common/src/tvosX64Main/kotlin/Posix.kt
+++ b/kmqtt-common/src/tvosX64Main/kotlin/Posix.kt
@@ -127,11 +127,19 @@ public actual fun getEagain(): Int = EAGAIN
 
 public actual fun getEwouldblock(): Int = EWOULDBLOCK
 
-public actual fun MemScope.set_socket_timeout(__fd: Int, timeout: Long): Int {
+public actual fun MemScope.set_send_socket_timeout(__fd: Int, timeout: Long): Int {
     val timeoutStruct = alloc<timeval>()
     val seconds = timeout / 1000
-    timeoutStruct.tv_sec = seconds
-    timeoutStruct.tv_usec = (timeout.toInt() - seconds.toInt() * 1000) * 1000
+    timeoutStruct.tv_sec = seconds.convert()
+    timeoutStruct.tv_usec = ((timeout - seconds * 1000) * 1000).convert()
+    return setsockopt(__fd, SOL_SOCKET, SO_SNDTIMEO, timeoutStruct.ptr, sizeOf<timeval>().toUInt())
+}
+
+public actual fun MemScope.set_recv_socket_timeout(__fd: Int, timeout: Long): Int {
+    val timeoutStruct = alloc<timeval>()
+    val seconds = timeout / 1000
+    timeoutStruct.tv_sec = seconds.convert()
+    timeoutStruct.tv_usec = ((timeout - seconds * 1000) * 1000).convert()
     return setsockopt(__fd, SOL_SOCKET, SO_RCVTIMEO, timeoutStruct.ptr, sizeOf<timeval>().toUInt())
 }
 
diff --git a/kmqtt-common/src/watchosArm32Main/kotlin/Posix.kt b/kmqtt-common/src/watchosArm32Main/kotlin/Posix.kt
index e4637da09f3ac9f411b1cbb58b827c6472d2e198..5d13333de4536de09b5b7304d8ab6494975bdb6b 100644
--- a/kmqtt-common/src/watchosArm32Main/kotlin/Posix.kt
+++ b/kmqtt-common/src/watchosArm32Main/kotlin/Posix.kt
@@ -127,11 +127,19 @@ public actual fun getEagain(): Int = EAGAIN
 
 public actual fun getEwouldblock(): Int = EWOULDBLOCK
 
-public actual fun MemScope.set_socket_timeout(__fd: Int, timeout: Long): Int {
+public actual fun MemScope.set_send_socket_timeout(__fd: Int, timeout: Long): Int {
     val timeoutStruct = alloc<timeval>()
     val seconds = timeout / 1000
-    timeoutStruct.tv_sec = seconds.toInt()
-    timeoutStruct.tv_usec = (timeout.toInt() - seconds.toInt() * 1000) * 1000
+    timeoutStruct.tv_sec = seconds.convert()
+    timeoutStruct.tv_usec = ((timeout - seconds * 1000) * 1000).convert()
+    return setsockopt(__fd, SOL_SOCKET, SO_SNDTIMEO, timeoutStruct.ptr, sizeOf<timeval>().toUInt())
+}
+
+public actual fun MemScope.set_recv_socket_timeout(__fd: Int, timeout: Long): Int {
+    val timeoutStruct = alloc<timeval>()
+    val seconds = timeout / 1000
+    timeoutStruct.tv_sec = seconds.convert()
+    timeoutStruct.tv_usec = ((timeout - seconds * 1000) * 1000).convert()
     return setsockopt(__fd, SOL_SOCKET, SO_RCVTIMEO, timeoutStruct.ptr, sizeOf<timeval>().toUInt())
 }
 
diff --git a/kmqtt-common/src/watchosArm64Main/kotlin/Posix.kt b/kmqtt-common/src/watchosArm64Main/kotlin/Posix.kt
index e4637da09f3ac9f411b1cbb58b827c6472d2e198..5d13333de4536de09b5b7304d8ab6494975bdb6b 100644
--- a/kmqtt-common/src/watchosArm64Main/kotlin/Posix.kt
+++ b/kmqtt-common/src/watchosArm64Main/kotlin/Posix.kt
@@ -127,11 +127,19 @@ public actual fun getEagain(): Int = EAGAIN
 
 public actual fun getEwouldblock(): Int = EWOULDBLOCK
 
-public actual fun MemScope.set_socket_timeout(__fd: Int, timeout: Long): Int {
+public actual fun MemScope.set_send_socket_timeout(__fd: Int, timeout: Long): Int {
     val timeoutStruct = alloc<timeval>()
     val seconds = timeout / 1000
-    timeoutStruct.tv_sec = seconds.toInt()
-    timeoutStruct.tv_usec = (timeout.toInt() - seconds.toInt() * 1000) * 1000
+    timeoutStruct.tv_sec = seconds.convert()
+    timeoutStruct.tv_usec = ((timeout - seconds * 1000) * 1000).convert()
+    return setsockopt(__fd, SOL_SOCKET, SO_SNDTIMEO, timeoutStruct.ptr, sizeOf<timeval>().toUInt())
+}
+
+public actual fun MemScope.set_recv_socket_timeout(__fd: Int, timeout: Long): Int {
+    val timeoutStruct = alloc<timeval>()
+    val seconds = timeout / 1000
+    timeoutStruct.tv_sec = seconds.convert()
+    timeoutStruct.tv_usec = ((timeout - seconds * 1000) * 1000).convert()
     return setsockopt(__fd, SOL_SOCKET, SO_RCVTIMEO, timeoutStruct.ptr, sizeOf<timeval>().toUInt())
 }
 
diff --git a/kmqtt-common/src/watchosSimulatorArm64Main/kotlin/Posix.kt b/kmqtt-common/src/watchosSimulatorArm64Main/kotlin/Posix.kt
index 305ae391fd9a4035371d8035e08a79ae869dcbf0..5d13333de4536de09b5b7304d8ab6494975bdb6b 100644
--- a/kmqtt-common/src/watchosSimulatorArm64Main/kotlin/Posix.kt
+++ b/kmqtt-common/src/watchosSimulatorArm64Main/kotlin/Posix.kt
@@ -127,11 +127,19 @@ public actual fun getEagain(): Int = EAGAIN
 
 public actual fun getEwouldblock(): Int = EWOULDBLOCK
 
-public actual fun MemScope.set_socket_timeout(__fd: Int, timeout: Long): Int {
+public actual fun MemScope.set_send_socket_timeout(__fd: Int, timeout: Long): Int {
     val timeoutStruct = alloc<timeval>()
     val seconds = timeout / 1000
-    timeoutStruct.tv_sec = seconds
-    timeoutStruct.tv_usec = (timeout.toInt() - seconds.toInt() * 1000) * 1000
+    timeoutStruct.tv_sec = seconds.convert()
+    timeoutStruct.tv_usec = ((timeout - seconds * 1000) * 1000).convert()
+    return setsockopt(__fd, SOL_SOCKET, SO_SNDTIMEO, timeoutStruct.ptr, sizeOf<timeval>().toUInt())
+}
+
+public actual fun MemScope.set_recv_socket_timeout(__fd: Int, timeout: Long): Int {
+    val timeoutStruct = alloc<timeval>()
+    val seconds = timeout / 1000
+    timeoutStruct.tv_sec = seconds.convert()
+    timeoutStruct.tv_usec = ((timeout - seconds * 1000) * 1000).convert()
     return setsockopt(__fd, SOL_SOCKET, SO_RCVTIMEO, timeoutStruct.ptr, sizeOf<timeval>().toUInt())
 }
 
diff --git a/kmqtt-common/src/watchosX64Main/kotlin/Posix.kt b/kmqtt-common/src/watchosX64Main/kotlin/Posix.kt
index 305ae391fd9a4035371d8035e08a79ae869dcbf0..5d13333de4536de09b5b7304d8ab6494975bdb6b 100644
--- a/kmqtt-common/src/watchosX64Main/kotlin/Posix.kt
+++ b/kmqtt-common/src/watchosX64Main/kotlin/Posix.kt
@@ -127,11 +127,19 @@ public actual fun getEagain(): Int = EAGAIN
 
 public actual fun getEwouldblock(): Int = EWOULDBLOCK
 
-public actual fun MemScope.set_socket_timeout(__fd: Int, timeout: Long): Int {
+public actual fun MemScope.set_send_socket_timeout(__fd: Int, timeout: Long): Int {
     val timeoutStruct = alloc<timeval>()
     val seconds = timeout / 1000
-    timeoutStruct.tv_sec = seconds
-    timeoutStruct.tv_usec = (timeout.toInt() - seconds.toInt() * 1000) * 1000
+    timeoutStruct.tv_sec = seconds.convert()
+    timeoutStruct.tv_usec = ((timeout - seconds * 1000) * 1000).convert()
+    return setsockopt(__fd, SOL_SOCKET, SO_SNDTIMEO, timeoutStruct.ptr, sizeOf<timeval>().toUInt())
+}
+
+public actual fun MemScope.set_recv_socket_timeout(__fd: Int, timeout: Long): Int {
+    val timeoutStruct = alloc<timeval>()
+    val seconds = timeout / 1000
+    timeoutStruct.tv_sec = seconds.convert()
+    timeoutStruct.tv_usec = ((timeout - seconds * 1000) * 1000).convert()
     return setsockopt(__fd, SOL_SOCKET, SO_RCVTIMEO, timeoutStruct.ptr, sizeOf<timeval>().toUInt())
 }