Fix race problem with detecting listening inetd sockets

The previous detection would fail if the socket closed before we
had time to inspect it, which got us stuck in a loop as we would
try (and fail) to do accept() on a non-listening socket.
diff --git a/common/network/TcpSocket.cxx b/common/network/TcpSocket.cxx
index 6561f3d..cf03c10 100644
--- a/common/network/TcpSocket.cxx
+++ b/common/network/TcpSocket.cxx
@@ -360,18 +360,14 @@
 #endif
 }
 
-bool TcpSocket::isSocket(int sock)
+bool TcpSocket::isListening(int sock)
 {
-  vnc_sockaddr_t sa;
-  socklen_t sa_size = sizeof(sa);
-  return getsockname(sock, &sa.u.sa, &sa_size) >= 0;
-}
-
-bool TcpSocket::isConnected(int sock)
-{
-  vnc_sockaddr_t sa;
-  socklen_t sa_size = sizeof(sa);
-  return getpeername(sock, &sa.u.sa, &sa_size) >= 0;
+  int listening = 0;
+  socklen_t listening_size = sizeof(listening);
+  if (getsockopt(sock, SOL_SOCKET, SO_ACCEPTCONN,
+                 (char *)&listening, &listening_size) < 0)
+    return false;
+  return listening != 0;
 }
 
 int TcpSocket::getSockPort(int sock)
diff --git a/common/network/TcpSocket.h b/common/network/TcpSocket.h
index 02f04c9..a97e683 100644
--- a/common/network/TcpSocket.h
+++ b/common/network/TcpSocket.h
@@ -65,8 +65,7 @@
 
     static bool enableNagles(int sock, bool enable);
     static bool cork(int sock, bool enable);
-    static bool isSocket(int sock);
-    static bool isConnected(int sock);
+    static bool isListening(int sock);
     static int getSockPort(int sock);
   private:
     bool closeFd;