+ /* On Windows, SO_REUSEADDR has the semantics we want for UDP
+ * sockets, but has nasty side effects we don't want for TCP
+ * sockets.
+ *
+ * On other platforms, we set SO_REUSEPORT, if it exists, for
+ * UDP sockets, and SO_REUSEADDR for all sockets, hoping that
+ * if SO_REUSEPORT doesn't exist, then SO_REUSEADDR will have
+ * the desired semantics on UDP (as it does on Linux, although
+ * Linux has SO_REUSEPORT too as of 3.9).
+ */
+
+#ifdef G_OS_WIN32
+ so_reuseaddr = reuse_address && (socket->priv->type == G_SOCKET_TYPE_DATAGRAM);
+#else
+ so_reuseaddr = !!reuse_address;
+#endif
+
+#ifdef SO_REUSEPORT
+ so_reuseport = reuse_address && (socket->priv->type == G_SOCKET_TYPE_DATAGRAM);
+#endif
+
+ /* Ignore errors here, the only likely error is "not supported", and
+ * this is a "best effort" thing mainly.
+ */
+ g_socket_set_option (socket, SOL_SOCKET, SO_REUSEADDR, so_reuseaddr, NULL);
+#ifdef SO_REUSEPORT
+ g_socket_set_option (socket, SOL_SOCKET, SO_REUSEPORT, so_reuseport, NULL);
+#endif
+