+ SYNCHRONIZE(synchronized);
+ int ret = _connect_socket(client);
+ DESYNCHRONIZE(synchronized);
+ return ret;
+}
+
+#ifndef WIN32
+static int _connect_socket(struct ClientSocket *client) {
+ if((client->flags & SOCKET_FLAG_CONNECTED)) return 1;
+ int sock;
+
+ struct addrinfo hints, *res;
+ struct sockaddr_in *ip4 = NULL;
+ struct sockaddr_in6 *ip6 = NULL;
+ memset (&hints, 0, sizeof (hints));
+ hints.ai_family = PF_UNSPEC;
+ hints.ai_socktype = SOCK_STREAM;
+ hints.ai_flags |= AI_CANONNAME;
+ if (getaddrinfo (client->host, NULL, &hints, &res)) {
+ return 0;
+ }
+ while (res) {
+ switch (res->ai_family) {
+ case AF_INET:
+ ip4 = (struct sockaddr_in *) res->ai_addr;
+ break;
+ case AF_INET6:
+ ip6 = (struct sockaddr_in6 *) res->ai_addr;
+ break;
+ }
+ res = res->ai_next;
+ }
+
+ if(ip6) {
+ sock = socket(AF_INET6, SOCK_STREAM, 0);
+ if(sock == -1) {
+ perror("socket() failed");
+ return 0;
+ }
+
+ ip6->sin6_family = AF_INET6;
+ ip6->sin6_port = htons(client->port);
+
+ struct sockaddr_in6 *ip6vhost = NULL;
+ if (client->bind && !getaddrinfo(client->bind, NULL, &hints, &res)) {
+ while (res) {
+ switch (res->ai_family) {
+ case AF_INET6:
+ ip6vhost = (struct sockaddr_in6 *) res->ai_addr;
+ break;
+ }
+ res = res->ai_next;
+ }
+ }
+ if(ip6vhost) {
+ ip6vhost->sin6_family = AF_INET6;
+ ip6vhost->sin6_port = htons(0);
+ bind(sock, (struct sockaddr*)ip6vhost, sizeof(*ip6vhost));
+ }
+
+ if (connect(sock, (struct sockaddr*)ip6, sizeof(*ip6)) == -1) {
+ perror("connect() failed");
+ return 0;
+ }
+
+ } else if(ip4) {
+ sock = socket(AF_INET, SOCK_STREAM, 0);
+ if(sock == -1) {
+ perror("socket() failed");
+ return 0;
+ }
+
+ ip4->sin_family = AF_INET;
+ ip4->sin_port = htons(client->port);
+
+ struct sockaddr_in *ip4vhost = NULL;
+ if (client->bind && !getaddrinfo(client->bind, NULL, &hints, &res)) {
+ while (res) {
+ switch (res->ai_family) {
+ case AF_INET:
+ ip4vhost = (struct sockaddr_in *) res->ai_addr;
+ break;
+ }
+ res = res->ai_next;
+ }
+ }
+ if(ip4vhost) {
+ ip4vhost->sin_family = AF_INET;
+ ip4vhost->sin_port = htons(0);
+ bind(sock, (struct sockaddr*)ip4vhost, sizeof(*ip4vhost));
+ }
+
+ if (connect(sock, (struct sockaddr*)ip4, sizeof(*ip4)) == -1) {
+ perror("connect() failed");
+ return 0;
+ }
+
+ } else
+ return 0;
+
+ if(get_int_field("Sockets.NoDelay")) {
+ int flag = 1;
+ if(setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, (char *) &flag, sizeof(int)) == -1) {
+ perror("setsockopt() failed");
+ return 0;
+ }
+ }
+
+ client->sock = sock;
+ client->flags |= SOCKET_FLAG_CONNECTED | SOCKET_FLAG_RECONNECT;
+ client->connection_time = time(0);
+
+ if(client->flags & SOCKET_FLAG_SSL) {
+ ssl_connect(client);
+ client->flags |= SOCKET_FLAG_HAVE_SSL;
+ } else
+ client->flags &= ~SOCKET_FLAG_HAVE_SSL;
+
+ //send the IRC Headers
+ char sendBuf[512];
+ int len;
+
+ if(client->pass && strcmp(client->pass, "")) {
+ len = sprintf(sendBuf, "PASS :%s\n", client->pass);
+ write_socket(client, sendBuf, len);
+ }
+ len = sprintf(sendBuf, "USER %s 0 0 :%s\n", client->ident, client->realname);
+ write_socket(client, sendBuf, len);
+ len = sprintf(sendBuf, "NICK %s\n", client->nick);
+ write_socket(client, sendBuf, len);
+
+ return 1;
+}
+#else
+static int _connect_socket(struct ClientSocket *client) {