title: Inconsistent behavior with Java ApacheCommonsNet TelnetClient
body: <p>I am using the code below for telnet operations. The issue is that if I do not use setSoTimeout(), it successfuly connects to Telnet Server everytime; but, if it is used, the frequency of successful connection reduces to 50%. Is there anything wrong in the manner in which it has been used? </p>
  
  <p>I actualy need the socket to throw timeout exception for different reasons and so I need to use setSoTimeout. Please suggest how to use it in a manner that it works effectively.</p>
  
  <pre><code>        public class TelnetClientHandle {
  
              private TelnetClient telnet = new TelnetClient();
              private InputStream in;
              private PrintStream out;
  
              public TelnetClientHandle(String server, String user, String password) {
  
                  try {
                      telnet.setDefaultTimeout(20000);
                      try {
                          telnet.connect(server, 23, sourceIP, 0);
                      } catch (Exception e) {
                          return_value = "Unable to establish Telnet Connection with Server";
                          return;
                          }
                  }
              }
  
                      in = telnet.getInputStream();
                      out = new PrintStream(telnet.getOutputStream());
  
                      String read_value = readUntil("login", true);
                      System.out.println(read_value);
                      if (read_value.contains("login")) {
                          write(user);
                          read_value = readUntil("sword", false);
                          System.out.println(read_value);
                          write(password);
                      } else {
                          return_value = "Unable to establish Telnet Connection with Server";
                          return;
                      }
  
                      telnet.setSoTimeout(2000);
                      read_value = readUntil("ABCDEFGH", false);
                      System.out.println(read_value);
                      if ((read_value == null)
                              || ((read_value != null &amp;&amp; !(read_value.toLowerCase()
                                      .contains("last login") || read_value
                                      .toLowerCase().contains("microsoft"))))) {
                          return_value = "Login error: Incorrect credentials or Server denied connection";
                          return;
                      }
  
                  } catch (Exception e) {
                      return_value = "Exception occurred: " + e.getMessage();
                  }
              }
  
              public String readUntil(String pattern, boolean checkForAvailableStream) {
                  StringBuffer sb = null;
                  try {
                      char lastChar = pattern.charAt(pattern.length() - 1);
                      sb = new StringBuffer();
                      char ch = (char) in.read();
                      while (true) {
                          sb.append(ch);
                          if (ch == lastChar) {
                              if (sb.toString().endsWith(pattern)) {
                                  return sb.toString();
                              }
                          }
                          if ((checkForAvailableStream) &amp;&amp; (in.available() &gt; 0)) {
                              ch = (char) in.read();
                          } else if (!checkForAvailableStream) {
  
                              ch = (char) in.read();
                          } else
                              break;
                      }
                  } catch (Exception e) {
                  }
                  return sb.toString();
              }
  
              public void write(String value) {
                  try {
                      out.write((value + "\r\n").getBytes("CP1252"));
                      out.flush();
                  } catch (Exception e) {                                        return_value = "Exception occurred in sending command to Server: "
                              + e.getMessage();
                  }
              }
  
              public void disconnect() {
                  try {
                      telnet.disconnect();
                  } catch (Exception e) {
                      "Exception occurred: " + e.getMessage());
                  }
              }
          }
  } 
  </code></pre>