session server BUGFIX limit number of connect retries

... with a single socket.
Refs cesnet/netopeer2#823
diff --git a/src/session_client.c b/src/session_client.c
index 941f872..df7e2f9 100644
--- a/src/session_client.c
+++ b/src/session_client.c
@@ -1257,7 +1257,7 @@
     char str[INET6_ADDRSTRLEN];
 
     if (sock_pending && *sock_pending != -1) {
-        VRB("Trying to connect the pending socket=%d.", *sock_pending );
+        VRB("Trying to connect the pending socket %d.", *sock_pending );
         sock = *sock_pending;
     } else {
         assert(res);
diff --git a/src/session_p.h b/src/session_p.h
index f49c807..d342ddb 100644
--- a/src/session_p.h
+++ b/src/session_p.h
@@ -262,6 +262,7 @@
             const char *address;
             uint16_t port;
             int sock_pending;
+            int sock_retries;
             struct nc_keepalives ka;
             union {
 #ifdef NC_ENABLED_SSH
@@ -342,6 +343,16 @@
 #define NC_REVERSE_QUEUE 5
 
 /**
+ * Timeout for connecting Call Home socket to a client (s).
+ */
+#define NC_SOCKET_CH_TIMEOUT 5
+
+/**
+ * Number of retires of connection Call Home socket to a client.
+ */
+#define NC_SOCKET_CH_RETRIES 5
+
+/**
  * @brief Type of the session
  */
 typedef enum {
diff --git a/src/session_server.c b/src/session_server.c
index 9c1698e..6ba051a 100644
--- a/src/session_server.c
+++ b/src/session_server.c
@@ -3075,8 +3075,15 @@
     struct timespec ts_cur;
     char *ip_host;
 
-    sock = nc_sock_connect(endpt->address, endpt->port, 5, &endpt->ka, &endpt->sock_pending, &ip_host);
+    sock = nc_sock_connect(endpt->address, endpt->port, NC_SOCKET_CH_TIMEOUT, &endpt->ka, &endpt->sock_pending, &ip_host);
     if (sock < 0) {
+        ++endpt->sock_retries;
+        if (endpt->sock_retries == NC_SOCKET_CH_RETRIES) {
+            ERR("Failed to connect socket %d after %d retries, closing.", endpt->sock_pending, NC_SOCKET_CH_RETRIES);
+            close(endpt->sock_pending);
+            endpt->sock_pending = -1;
+            endpt->sock_retries = 0;
+        }
         return NC_MSG_ERROR;
     }
     /* no need to store the socket as pending any longer */