+static void configure_tcp(connection_t *c) {
+#ifdef O_NONBLOCK
+ int flags = fcntl(c->socket, F_GETFL);
+
+ if(fcntl(c->socket, F_SETFL, flags | O_NONBLOCK) < 0) {
+ logger(c->mesh, MESHLINK_ERROR, "System call `%s' failed: %s", "fcntl", strerror(errno));
+ }
+
+#elif defined(WIN32)
+ unsigned long arg = 1;
+
+ if(ioctlsocket(c->socket, FIONBIO, &arg) != 0) {
+ logger(c->mesh, MESHLINK_ERROR, "System call `%s' failed: %s", "ioctlsocket", sockstrerror(sockerrno));
+ }
+
+#endif
+
+#if defined(SOL_TCP) && defined(TCP_NODELAY)
+ int nodelay = 1;
+ setsockopt(c->socket, SOL_TCP, TCP_NODELAY, (void *)&nodelay, sizeof(nodelay));
+#endif
+
+#if defined(IP_TOS) && defined(IPTOS_LOWDELAY)
+ int lowdelay = IPTOS_LOWDELAY;
+ setsockopt(c->socket, IPPROTO_IP, IP_TOS, (void *)&lowdelay, sizeof(lowdelay));
+#endif
+}
+
+static bool bind_to_address(meshlink_handle_t *mesh, connection_t *c) {
+ int s = -1;
+
+ for(int i = 0; i < mesh->listen_sockets && mesh->listen_socket[i].bindto; i++) {
+ if(mesh->listen_socket[i].sa.sa.sa_family != c->address.sa.sa_family) {
+ continue;
+ }
+
+ if(s >= 0) {
+ return false;
+ }
+
+ s = i;
+ }
+
+ if(s < 0) {
+ return false;
+ }
+
+ sockaddr_t sa = mesh->listen_socket[s].sa;
+
+ if(sa.sa.sa_family == AF_INET) {
+ sa.in.sin_port = 0;
+ } else if(sa.sa.sa_family == AF_INET6) {
+ sa.in6.sin6_port = 0;
+ }
+
+ if(bind(c->socket, &sa.sa, SALEN(sa.sa))) {
+ logger(mesh, MESHLINK_WARNING, "Can't bind outgoing socket: %s", strerror(errno));
+ return false;
+ }
+
+ return true;
+}
+
+int setup_listen_socket(const sockaddr_t *sa) {
+ int nfd;