+ net_connect_abort(pfds);
+ return -1;
+}
+
+struct addrinfo *net_server_lookup(const char *service,
+ int family,
+ int socktype)
+{
+ struct addrinfo *res, hints;
+
+ memset(&hints, 0, sizeof(hints));
+ hints.ai_family = family;
+ hints.ai_socktype = socktype;
+ hints.ai_flags = AI_PASSIVE;
+ hints.ai_protocol = 0;
+
+ if (getaddrinfo(NULL, service, &hints, &res) != 0)
+ return NULL;
+
+ return res;
+}
+
+static bool should_listen(const struct addrinfo *addrinfo)
+{
+#ifdef SOCK_SEQPACKET
+ if (addrinfo->ai_socktype == SOCK_SEQPACKET)
+ return true;
+#endif
+ return (addrinfo->ai_socktype == SOCK_STREAM);
+}
+
+static int make_listen_fd(const struct addrinfo *addrinfo)
+{
+ int saved_errno, fd, on = 1;
+
+ fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
+ addrinfo->ai_protocol);
+ if (fd < 0)
+ return -1;
+
+ setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
+ if (bind(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0)
+ goto fail;
+
+ if (should_listen(addrinfo) && listen(fd, 5) != 0)
+ goto fail;
+ return fd;