+ else
+ {
+ save_errno = errno;
+ if (!silent)
+ logprintf (LOG_VERBOSE, "failed: %s.\n", strerror (errno));
+ errno = save_errno;
+ }
+
+ return sock;
+}
+
+/* Connect to a remote host whose address has been resolved. */
+int
+connect_to_many (struct address_list *al, unsigned short port, int silent)
+{
+ int i, start, end;
+
+ address_list_get_bounds (al, &start, &end);
+ for (i = start; i < end; i++)
+ {
+ ip_address addr;
+ int sock;
+ address_list_copy_one (al, i, &addr);
+
+ sock = connect_to_one (&addr, port, silent);
+ if (sock >= 0)
+ /* Success. */
+ return sock;
+
+ address_list_set_faulty (al, i);
+
+ /* The attempt to connect has failed. Continue with the loop
+ and try next address. */
+ }
+
+ return -1;
+}
+
+int
+test_socket_open (int sock)
+{
+#ifdef HAVE_SELECT
+ fd_set check_set;
+ struct timeval to;
+
+ /* Check if we still have a valid (non-EOF) connection. From Andrew
+ * Maholski's code in the Unix Socket FAQ. */
+
+ FD_ZERO (&check_set);
+ FD_SET (sock, &check_set);
+
+ /* Wait one microsecond */
+ to.tv_sec = 0;
+ to.tv_usec = 1;
+
+ /* If we get a timeout, then that means still connected */
+ if (select (sock + 1, &check_set, NULL, NULL, &to) == 0)
+ {
+ /* Connection is valid (not EOF), so continue */
+ return 1;
+ }
+ else
+ return 0;
+#else
+ /* Without select, it's hard to know for sure. */
+ return 1;
+#endif