Response response = new Response();
response.server = server;
response.port = port;
Socket connection = AsyncSocketFactory.createSocket(server, port, timeout);
try {
PrintStream out = new PrintStream(connection.getOutputStream());
try {
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
try {
out.println(query);
StringBuffer sb = new StringBuffer();
String line;
while ((line = in.readLine()) != null) {
sb.append(line).append(lineSeparator);
line = line.trim();
if (! line.startsWith("%") && ! line.startsWith("#")) {
int fs = line.indexOf(":");
if (fs > 0) {
String name = line.substring(0, fs);
String value = line.substring(fs + 1).trim();
response.data.put(name, value);
}
}
}
response.summary = sb.toString();
Response newResponse = null;
String referral = (String) response.getData().get("ReferralServer");
if (referral != null) {
try {
URLParser url = new URLParser(referral);
if ("whois".equals(url.getProtocol())) {
newResponse = lookup(url.getHost(), url.getPort() == -1 ? 43 : url.getPort(), query,
timeout, lineSeparator);
}
} catch (IOException e) {
System.out.println("Could not contact " + referral);
}
}
if (newResponse != null) {
response = newResponse;
}
} finally {
in.close();
}
} finally {
out.close();
}
} finally {
connection.close();
}
return response;
}