From 0aa31c054885a122019fcdd2472742ebf6fe06ab Mon Sep 17 00:00:00 2001
From: Zach Klippenstein <zach.klippenstein@gmail.com>
Date: Mon, 7 Sep 2015 16:06:20 -0700
Subject: [PATCH] If dialing the server fails initially, attempt to start the
 server and retry.

---
 dialer.go      | 11 ++++++++++-
 host_client.go |  2 +-
 2 files changed, 11 insertions(+), 2 deletions(-)

diff --git a/dialer.go b/dialer.go
index 225a79b..ebb5c12 100644
--- a/dialer.go
+++ b/dialer.go
@@ -54,7 +54,16 @@ func (d *netDialer) Dial() (*wire.Conn, error) {
 	address := fmt.Sprintf("%s:%d", host, port)
 	netConn, err := net.Dial("tcp", address)
 	if err != nil {
-		return nil, util.WrapErrorf(err, util.ServerNotAvailable, "error dialing %s", address)
+		// Attempt to start the server and try again.
+		if err = StartServer(); err != nil {
+			return nil, util.WrapErrorf(err, util.ServerNotAvailable, "error starting server")
+		}
+
+		address = fmt.Sprintf("%s:%d", host, port)
+		netConn, err = net.Dial("tcp", address)
+		if err != nil {
+			return nil, util.WrapErrorf(err, util.ServerNotAvailable, "error dialing %s", address)
+		}
 	}
 
 	conn := &wire.Conn{
diff --git a/host_client.go b/host_client.go
index c74126e..ebca1b7 100644
--- a/host_client.go
+++ b/host_client.go
@@ -11,8 +11,8 @@ import (
 HostClient communicates with host services on the adb server.
 
 Eg.
+	StartServer()
 	client := NewHostClient()
-	client.StartServer()
 	client.ListDevices()
 
 See list of services at https://android.googlesource.com/platform/system/core/+/master/adb/SERVICES.TXT.