// limit the subset here as we're looping waiting for something to be alive and we can't afford to take ages getting back to the start
long start = System.currentTimeMillis();
while( true ){
for (int i=45100;i<=45108;i++){
long now = System.currentTimeMillis();
if ( now < start ){
start = now;
}
if ( now - start > max_millis_to_wait ){
return( false );
}
Socket sock = null;
try{
sock = new Socket();
System.out.println( "trying " + i );
sock.connect( new InetSocketAddress( "127.0.0.1", i ), 500 );
sock.setSoTimeout( 5000 );
PrintWriter pw = new PrintWriter( sock.getOutputStream());
pw.println( "GET /select/" + magnet_uri + NL + NL );
pw.flush();
InputStream is = sock.getInputStream();
String res = "";
byte[] buffer = new byte[1024];
while( true ){
int len = is.read( buffer );
if ( len <= 0 ){
break;
}
res += new String( buffer, 0, len );
if ( res.indexOf( " 200" ) != -1 ){
return( true );
}
}
}catch( Throwable e ){
}finally{
if ( sock != null ){
try{
sock.close();
}catch( Throwable e ){
}
}
}
}
}