title: How to send via a TCP socket in Kotlin (android)?
body: <p>I hoped that if there was a <code>java.net.ConnectException</code>, the <code>onFailure</code> block would be called. But It doesn't work as expected.</p>
  
  <p>Could you help?</p>
  
  <pre><code>        Socket(ip, port).use { socket: Socket -&gt;
              val outputStream = socket.getOutputStream()
              outputStream.write(valueOn)
              outputStream.flush()
              state = State.FINISHED
          }.runCatching {
              state = State.FAILED
          }.onFailure {
              state = State.FAILED
          }
  </code></pre>