- int n;
- struct addrinfo hints, *res, *ressave;
- char tcp_port_rdp_s[10];
-
- snprintf(tcp_port_rdp_s, 10, "%d", tcp_port_rdp);
-
- memset(&hints, 0, sizeof(struct addrinfo));
- hints.ai_family = AF_UNSPEC;
- hints.ai_socktype = SOCK_STREAM;
-
- n = getaddrinfo(server, tcp_port_rdp_s, &hints, &res);
+ int n;
+ struct addrinfo hints, *res, *ressave;
+ char tcp_port_rdp_s[10];
+
+ snprintf(tcp_port_rdp_s, 10, "%d", tcp_port_rdp);
+
+ memset(&hints, 0, sizeof(struct addrinfo));
+ hints.ai_family = AF_UNSPEC;
+ hints.ai_socktype = SOCK_STREAM;
+
+ n = getaddrinfo(server, tcp_port_rdp_s, &hints, &res);
+
+ if (n < 0)
+ {
+ error("getaddrinfo: %s\n", gai_strerror(n));
+ return -1;
+ }
+
+ ressave = res;
+ sock = -1;
+ while (res)
+ {
+ sock = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
+ if (!(sock < 0))
+ {
+ if (connect(sock, res->ai_addr, res->ai_addrlen) == 0)
+ break;
+ close(sock);
+ sock = -1;
+ }
+ res = res->ai_next;
+ }
+ freeaddrinfo(ressave);
+
+ if (sock == -1)
+ {
+ error("%s: unable to connect\n", server);
+ return -1;
+ }