// 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 ){
long connect_start = System.currentTimeMillis();
if ( connect_start < start ){
start = connect_start;
}
if ( connect_start - start > max_millis_to_wait ){
return( false );
}
Socket sock = null;
try{
sock = new Socket();
sock.connect( new InetSocketAddress( "127.0.0.1", 6880 ), CONNECT_TIMEOUT );
sock.setSoTimeout( READ_TIMEOUT );
PrintWriter pw = new PrintWriter(new OutputStreamWriter(sock.getOutputStream(),"UTF-8"));
StringBuffer buffer = new StringBuffer( ACCESS_STRING + ";args;");
for ( int i = 0 ; i < args.length ; i++ ){
String arg = args[i].replaceAll("&","&&").replaceAll(";","&;");
buffer.append(arg);
buffer.append(';");
}
pw.println(buffer.toString());
pw.flush();
return( true );
}catch( Throwable e ){
long connect_end = System.currentTimeMillis();
long time_taken = connect_end - connect_start;
if ( time_taken < CONNECT_TIMEOUT ){
try{
Thread.sleep( CONNECT_TIMEOUT - time_taken );
}catch( Throwable f ){
}
}
}finally{
try{
if ( sock != null ){
sock.close();
}
}catch( Throwable e ){
}
}
}