{ Resolves the given hostname, and tries to connect it on the given port. }
var GAIResult: TGAIResult;
begin
- FSocket:= fpSocket(af_inet, sock_stream, 0);
- if (FSocket <> -1) then begin
- GAIResult:= ResolveHost(HostName, AF_UNSPEC);
- if GAIResult.GAIError = 0 then begin
- Move(GAIResult.AddrInfo^.ai_addr^, SockAddr, GAIResult.AddrInfo^.ai_addrlen);
- SockAddr.sin6_port:= htons(Port);
+ GAIResult:= ResolveHost(HostName, AF_UNSPEC);
+ if GAIResult.GAIError = 0 then begin
+ Move(GAIResult.AddrInfo^.ai_addr^, SockAddr, GAIResult.AddrInfo^.ai_addrlen);
+ SockAddr.sin6_port:= htons(Port);
+
+ { Create socket. }
+ FSocket:= fpSocket(GAIResult.AddrInfo^.ai_family, SOCK_STREAM, 0);
+
+ if (FSocket <> -1) then begin
{ Try to initiate connection. }
FConnected:= fpConnect(FSocket, @SockAddr, GAIResult.AddrInfo^.ai_addrlen) <> -1;
- if FConnected then begin
- FHostIP:= TIPNamePair.Create(HostName, IPToStr(@SockAddr));
- SetSockTimeOut(FSockTimeOut);
- end
- else
- CloseSocket(FSocket);
+ if FConnected then begin
+ FHostIP:= TIPNamePair.Create(HostName, IPToStr(@SockAddr));
+ SetSockTimeOut(FSockTimeOut);
+ end
+ else
+ CloseSocket(FSocket);
- FreeHost(GAIResult);
end;
+
+ FreeHost(GAIResult);
end;
Result:= FConnected;
end;