- resp = res;
- while (1) {
- s = socket(resp->ai_family, resp->ai_socktype, resp->ai_protocol);
-
- if (s >= 0) {
- break; /* got a socket */
- } else if ((resp = resp->ai_next)) {
- switch (errno) {
- case EPROTONOSUPPORT:
- case EAFNOSUPPORT:
- case EPFNOSUPPORT:
- case EINVAL:
- /* See if there's another address that will work... */
- continue;
- }
+ /* We may not be able to create the socket, if for example the
+ * machine knows about IPv6 in the C library, but not in the
+ * kernel. */
+ for (resp = all_ai; resp; resp = resp->ai_next) {
+ s = socket(resp->ai_family, resp->ai_socktype,
+ resp->ai_protocol);
+
+ if (s == -1)
+ /* See if there's another address that will work... */
+ continue;
+
+ setsockopt(s, SOL_SOCKET, SO_REUSEADDR,
+ (char *)&one, sizeof one);
+
+ /* now we've got a socket - we need to bind it */
+ if (bind(s, all_ai->ai_addr, all_ai->ai_addrlen) < 0) {
+ /* Nope, try another */
+ close(s);
+ continue;