+ if((nfd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol)) < 0)
+ {
+ syslog(LOG_ERR, _("Creating metasocket failed: %m"));
+ continue;
+ }
+
+ flags = fcntl(nfd, F_GETFL);
+ if(fcntl(nfd, F_SETFL, flags | O_NONBLOCK) < 0)
+ {
+ close(nfd);
+ syslog(LOG_ERR, _("System call `%s' failed: %m"),
+ "fcntl");
+ continue;
+ }
+
+ /* Optimize TCP settings */
+
+ option = 1;
+ setsockopt(nfd, SOL_SOCKET, SO_REUSEADDR, &option, sizeof(option));
+ setsockopt(nfd, SOL_SOCKET, SO_KEEPALIVE, &option, sizeof(option));
+#ifdef HAVE_LINUX
+ setsockopt(nfd, SOL_TCP, TCP_NODELAY, &option, sizeof(option));
+
+ option = IPTOS_LOWDELAY;
+ setsockopt(nfd, SOL_IP, IP_TOS, &option, sizeof(option));
+
+ if(get_config_string(lookup_config(config_tree, "BindToInterface"), &interface))
+ if(setsockopt(nfd, SOL_SOCKET, SO_BINDTODEVICE, interface, strlen(interface)))
+ {
+ close(nfd);
+ syslog(LOG_ERR, _("Can't bind to interface %s: %m"), interface);
+ continue;
+ }
+#endif
+
+ if(bind(nfd, ai->ai_addr, ai->ai_addrlen))
+ {
+ close(nfd);
+ syslog(LOG_ERR, _("Can't bind to %s port %d/tcp: %m"),
+ ai->ai_canonname, n->port);
+ continue;
+ }
+
+ if(listen(nfd, 3))
+ {
+ close(nfd);
+ syslog(LOG_ERR, _("System call `%s' failed: %m"),
+ "listen");
+ continue;
+ }
+
+ break; /* We have successfully bound to a socket */