This approach prevents another process from attempting to close the socket and the stream at the same time.
B4X:
Private myclient As Socket
Private astream As AsyncStreams
Private IsClosing As Boolean
Public Sub CloseConnection
If IsClosing Then Return
IsClosing = True
If astream.IsInitialized Then astream.Close
If myclient.IsInitialized Then myclient.Close
IsClosing = False
End Sub