+wait_for_connect (struct serial *scb, int *polls)
+{
+ struct timeval t;
+ int n;
+
+ /* While we wait for the connect to complete,
+ poll the UI so it can update or the user can
+ interrupt. */
+ if (deprecated_ui_loop_hook && deprecated_ui_loop_hook (0))
+ {
+ errno = EINTR;
+ return -1;
+ }
+
+ /* Check for timeout. */
+ if (*polls > tcp_retry_limit * POLL_INTERVAL)
+ {
+ errno = ETIMEDOUT;
+ return -1;
+ }
+
+ /* Back off to polling once per second after the first POLL_INTERVAL
+ polls. */
+ if (*polls < POLL_INTERVAL)
+ {
+ t.tv_sec = 0;
+ t.tv_usec = 1000000 / POLL_INTERVAL;
+ }
+ else
+ {
+ t.tv_sec = 1;
+ t.tv_usec = 0;
+ }
+
+ if (scb)
+ {
+ fd_set rset, wset, eset;
+ FD_ZERO (&rset);
+ FD_SET (scb->fd, &rset);
+ wset = rset;
+ eset = rset;
+
+ /* POSIX systems return connection success or failure by signalling
+ wset. Windows systems return success in wset and failure in
+ eset.
+
+ We must call select here, rather than gdb_select, because
+ the serial structure has not yet been initialized - the
+ MinGW select wrapper will not know that this FD refers
+ to a socket. */
+ n = select (scb->fd + 1, &rset, &wset, &eset, &t);
+ }
+ else
+ /* Use gdb_select here, since we have no file descriptors, and on
+ Windows, plain select doesn't work in that case. */
+ n = gdb_select (0, NULL, NULL, NULL, &t);
+
+ /* If we didn't time out, only count it as one poll. */
+ if (n > 0 || *polls < POLL_INTERVAL)
+ (*polls)++;
+ else
+ (*polls) += POLL_INTERVAL;
+
+ return n;
+}
+
+/* Open a tcp socket */
+
+int