+jb_socket connect_to(const char *host, int portnum, struct client_state *csp)
+{
+ jb_socket fd;
+ int forwarded_connect_retries = 0;
+
+ do
+ {
+ /*
+ * XXX: The whole errno overloading is ridiculous and should
+ * be replaced with something sane and thread safe
+ */
+ /* errno = 0;*/
+#ifdef HAVE_RFC2553
+ fd = rfc2553_connect_to(host, portnum, csp);
+#else
+ fd = no_rfc2553_connect_to(host, portnum, csp);
+#endif
+ if ((fd != JB_INVALID_SOCKET) || (errno == EINVAL)
+ || (csp->fwd == NULL)
+ || ((csp->fwd->forward_host == NULL) && (csp->fwd->type == SOCKS_NONE)))
+ {
+ break;
+ }
+ forwarded_connect_retries++;
+ if (csp->config->forwarded_connect_retries != 0)
+ {
+ log_error(LOG_LEVEL_ERROR,
+ "Attempt %d of %d to connect to %s failed. Trying again.",
+ forwarded_connect_retries, csp->config->forwarded_connect_retries, host);
+ }
+
+ } while (forwarded_connect_retries < csp->config->forwarded_connect_retries);
+
+ return fd;
+}
+