title: How make this Android NodeMCU TCP socket code auto-reconnect?
body: <p>Use case: 
  NodeMCU connects to Android over TCP socket, then Android plays a sound, when user presses a button, which is connected to NodeMCU.</p>
  
  <p>I couldn't find a way (that I could believe as reliable) to have Android's ServerSocket to notice a reset from NodeMCU client socket, right at once, when NodeMCU's socket is disconnected, or trying to reconnect.</p>
  
  <p>This is my newbee socket code bellow, which I got it working also with the help of this forum. I tried to make it as short as possible when posting here, keeping only relevant parts, but if you need I can just drop all the code, so please let me know in case.</p>
  
  <p>Please suggest me a way. Any other comment to help me understand more of sockets is very welcome. </p>
  
  <p>I'd like to finally learn what sockets are!</p>
  
  <pre><code>public class MainActivity extends AppCompatActivity {
  ServerSocket serverSocket;
  Thread socketServerThread = null;
  
  protected void onCreate(Bundle savedInstanceState) {
      //...
      Globals.snd = MediaPlayer.create(this, R.raw.bike_horn);// Globals - static class
      //...
      socketServerThread = new Thread(new SocketServerThread());
      socketServerThread.start();
  }
  protected void onDestroy() {
      if (serverSocket != null) {...//try close it}
  }
  private class SocketServerThread extends Thread {
      //...
      static final int SocketServerPORT = 8080;
      public void run() {
          Socket socket = null;
          DataInputStream dataInputStream = null;
          DataOutputStream dataOutputStream = null;
          BufferedReader input = null;
          try {
              serverSocket = new ServerSocket();
              serverSocket.setReuseAddress(true);
              serverSocket.bind(new InetSocketAddress(SocketServerPORT));
              while (true) {
                  socket = serverSocket.accept();
                  dataInputStream = new DataInputStream(socket.getInputStream());
                  dataOutputStream = new DataOutputStream(socket.getOutputStream());
                  while(socket.isConnected()){
                      input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                      message = input.readLine();
                      if(message == null) break;
                      int x = Integer.valueOf(message);
                      if(x == 1) Globals.snd.start();
                  }
              }
          }catch (IOException e) {// print error stack
          }finally{//close socket, dataInputStream, dataOutputStream
          }
  </code></pre>
  
  <p>NodeMCU code:</p>
  
  <pre><code>#include &lt;ESP8266WiFi.h&gt;
  //...variables defined
  void setup() {
      //...connect to wifi
      client.connect(host, port);
  }
  void loop() {
      if(!client.connected()){
          if (!client.connect(host, port)) {
              return;
          }
      }
  
      while(digitalRead(beepBtnPin) == LOW &amp;&amp; client.connected()){
          client.println("2");
          client.flush();
          delay(500);
      }
  }
  </code></pre>