+ static bool supportsTcpReuse = false;
+ static bool supportsTcpReuseSet = false;
+
+ static bool SupportsTcpReuse ()
+ {
+ if (supportsTcpReuseSet)
+ return supportsTcpReuse;
+
+ if (Path.DirectorySeparatorChar == '/') {
+ /*
+ * On UNIX OS
+ * Multiple threads listening to the same address and port are not possible
+ * before linux 3.9 kernel, where the socket option SO_REUSEPORT was introduced.
+ */
+ Regex reg = new Regex(@"^#define\s*SO_REUSEPORT");
+ foreach (string directory in Directory.GetDirectories ("/usr/include")) {
+ var f = Directory.GetFiles (directory, "socket.h").SingleOrDefault ();
+ if (f != null && File.ReadLines (f).Any (l => reg.Match (l).Success)) {
+ supportsTcpReuse = true;
+ break;
+ }
+ }
+ } else {
+ supportsTcpReuse = true;
+ }
+
+ supportsTcpReuseSet = true;
+
+ return supportsTcpReuse;
+ }
+