title: LocalSocket write and flush ends in broken pipe error
body: <p>I'm trying to communicate with Chrome's developer tools using their socket's protocol inside Android. 
  The USB debugging option in chrome is enabled, and if I do a adb forward from my computer, I am able to interact with the socket. But I need to access it programatically inside the phone. </p>
  
  <p>Here's some code: </p>
  
  <pre><code>package something;
  
  import java.lang.StringBuilder;
  import java.io.IOException;
  import java.io.InputStream;
  import java.io.OutputStream;
  
  import android.os.Bundle;
  import android.os.Handler;
  import android.util.Log;
  import android.net.LocalServerSocket;
  import android.net.LocalSocket;
  import android.net.LocalSocketAddress;
  import android.app.Service;
  import android.content.Intent;
  import android.os.IBinder;
  import android.util.Log;
  import java.io.BufferedOutputStream;
  import java.io.BufferedInputStream;
  
  public class SomeService extends Service {
      private final String TAG = SomeService.class.toString();
      private int cnt = 0;
      public static String SOCKET_ADDRESS = "chrome_devtools_remote";
      private String message = "/json";
  
      @Override
      public IBinder onBind(Intent intent) {
          return null;
      }
  
      @Override
      public int onStartCommand(Intent intent, int flags, int startId) {
          Log.d(TAG, "service started");
          Thread t = new Thread(new Runnable() {
               public void run()
               {
                  try {
                      LocalSocket receiver = new LocalSocket(LocalSocket.SOCKET_STREAM);
                      receiver.connect(new LocalSocketAddress(SOCKET_ADDRESS, LocalSocketAddress.Namespace.ABSTRACT));
                      StringBuilder request = new StringBuilder().append("GET /json HTTP/1.0\r\n");
                      BufferedOutputStream outbuf = new BufferedOutputStream(receiver.getOutputStream());
                      BufferedInputStream inbuf = new BufferedInputStream(receiver.getInputStream());
                      byte[] buffer = request.toString().getBytes("utf-8");    
                      Log.d(TAG, "outbuf size " + Integer.toString(receiver.getSendBufferSize()));
                      while (true) {
                          Log.d(TAG, "sending " + request.toString());
                          outbuf.write(buffer, 0, buffer.length);
                          outbuf.flush();
                          Log.d(TAG, "sent!");
                          byte[] output = new byte[1000];
                          int read = inbuf.read();
                          int size = 0;
                          int capacity = 0;
                          byte[] bytes = new byte[capacity];
  
                          // reading
                          while (read != -1) {
                              Log.d(TAG, "recieving");
                              capacity = (capacity * 3)/2 + 1;
                              byte[] copy = new byte[capacity];
                              System.arraycopy(bytes, 0, copy, 0, bytes.length);
                              bytes = copy;
                              bytes[size++] = (byte)read;
  
                              // read next byte
                              read = inbuf.read();
                          }
                          Log.d(TAG, new String(bytes));
                          try {
                              Thread.sleep(3000);    
                          } catch(Exception e) {
  
                          }
  
                      }                 
  
                  } catch (IOException e) {
                      Log.e(getClass().getName(), e.getMessage(),e);
                  }
              }
          });
          t.start();
          return Service.START_STICKY;
      }
  }
  </code></pre>
  
  <p>This always results in this error: </p>
  
  <pre><code>Starting service: Intent { cmp=something/.MyService (has extras) }
  D/class something.MyService(18717): service started
  D/class something.MyService(18717): outbuf size 163840
  D/class something.MyService(18717): sending GET /json HTTP/1.0
  E/something.MyService$1(18717): Broken pipe
  E/something.MyService$1(18717): java.io.IOException: Broken pipe
  E/something.MyService$1(18717):  at android.net.LocalSocketImpl.writeba_native(Native Method)
  E/something.MyService$1(18717):  at android.net.LocalSocketImpl.access$600(LocalSocketImpl.java:33)
  E/something.MyService$1(18717):  at android.net.LocalSocketImpl$SocketOutputStream.write(LocalSocketImpl.java:134)
  E/something.MyService$1(18717):  at java.io.BufferedOutputStream.flushInternal(BufferedOutputStream.java:185)
  E/something.MyService$1(18717):  at java.io.BufferedOutputStream.flush(BufferedOutputStream.java:85)
  E/something.MyService$1(18717):  at something.MyService$1.run(RTCService.java:49)
  E/something.MyService$1(18717):  at java.lang.Thread.run(Thread.java:841)
  </code></pre>
  
  <p>The socket is definitely open, which I am checking using <code>isConnected()</code> method of the socket object. Any ideas what I am doing wrong?</p>