session CHANGE use more practical TCP keep alive options

Refs cesnet/netopeer2#292
diff --git a/src/session_client.c b/src/session_client.c
index 84f44c4..4d196ee 100644
--- a/src/session_client.c
+++ b/src/session_client.c
@@ -935,18 +935,7 @@
         /* make the socket non-blocking */
         if (((flags = fcntl(sock, F_GETFL)) == -1) || (fcntl(sock, F_SETFL, flags | O_NONBLOCK) == -1)) {
             ERR("Fcntl failed (%s).", strerror(errno));
-            close(sock);
-            freeaddrinfo(res_list);
-            return -1;
-        }
-
-        /* enable keep-alive */
-        i = 1;
-        if (setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, &i, sizeof i) == -1) {
-            ERR("Setsockopt failed (%s).", strerror(errno));
-            close(sock);
-            freeaddrinfo(res_list);
-            return -1;
+            goto error;
         }
 
         /* we're done, network connection established */
@@ -959,6 +948,13 @@
     freeaddrinfo(res_list);
 
     return sock;
+
+error:
+    if (sock > -1) {
+        close(sock);
+    }
+    freeaddrinfo(res_list);
+    return -1;
 }
 
 static NC_MSG_TYPE
diff --git a/src/session_server.c b/src/session_server.c
index 2f9aa5d..94156c0 100644
--- a/src/session_server.c
+++ b/src/session_server.c
@@ -21,6 +21,7 @@
 #include <sys/types.h>
 #include <sys/socket.h>
 #include <netinet/in.h>
+#include <netinet/tcp.h>
 #include <arpa/inet.h>
 #include <unistd.h>
 #include <fcntl.h>
@@ -163,8 +164,7 @@
 int
 nc_sock_listen(const char *address, uint16_t port)
 {
-    const int optVal = 1;
-    const socklen_t optLen = sizeof(optVal);
+    int opt;
     int is_ipv4, sock;
     struct sockaddr_storage saddr;
 
@@ -184,8 +184,14 @@
         goto fail;
     }
 
-    if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void *)&optVal, optLen) == -1) {
-        ERR("Could not set socket SO_REUSEADDR socket option (%s).", strerror(errno));
+    opt = 1;
+    if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof opt) == -1) {
+        ERR("Could not set SO_REUSEADDR socket option (%s).", strerror(errno));
+        goto fail;
+    }
+    opt = 1;
+    if (setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, &opt, sizeof opt) == -1) {
+        ERR("Could not set SO_KEEPALIVE option (%s).", strerror(errno));
         goto fail;
     }
 
@@ -332,7 +338,19 @@
 
     /* enable keep-alive */
     flags = 1;
-    if (setsockopt(ret, SOL_SOCKET, SO_KEEPALIVE, &flags, sizeof flags) == -1) {
+    if (setsockopt(ret, IPPROTO_TCP, TCP_KEEPIDLE, &flags, sizeof flags) == -1) {
+        ERR("Setsockopt failed (%s).", strerror(errno));
+        close(ret);
+        return -1;
+    }
+    flags = 5;
+    if (setsockopt(ret, IPPROTO_TCP, TCP_KEEPINTVL, &flags, sizeof flags) == -1) {
+        ERR("Setsockopt failed (%s).", strerror(errno));
+        close(ret);
+        return -1;
+    }
+    flags = 10;
+    if (setsockopt(ret, IPPROTO_TCP, TCP_KEEPCNT, &flags, sizeof flags) == -1) {
         ERR("Setsockopt failed (%s).", strerror(errno));
         close(ret);
         return -1;