+#ifdef _MSC_VER
+typedef void (__cdecl * sighandler_t)(int);
+#elif defined( __APPLE__ )
+typedef sig_t sighandler_t;
+#endif
+
+bool terminating = false;
+sighandler_t prior_signal_handlers[32];
+int termination_triggering_signals[] = {
+#ifndef _MSC_VER
+ SIGHUP, SIGINT, SIGQUIT, SIGKILL,
+#else
+ SIGINT, SIGILL, SIGFPE, SIGSEGV, SIGTERM, SIGBREAK, SIGABRT,
+#endif
+ 0}; // zero terminated
+
+void terminate_request_handler(int param) {
+ char msg[] = "\nReceived signal XX, intend to exit soon.\n"
+ "repeat the signal to force immediate termination.\n";
+ msg[17] = '0' + param / 10;
+ msg[18] = '0' + param % 10;
+ write(1, msg, sizeof(msg) - 1);
+ terminating = true;
+ signal(param, prior_signal_handlers[param]);
+ theSocket.close();
+}
+