I have a client that is sending (receiving) data to (from) a server. The code of the client is similar to:

```
conn, _ := net.Dial("tcp", "127.0.0.1:3456")
reader := bufio.NewReader(conn)
writer := bufio.NewWriter(conn)

for true {
   writer.write(data)
   reader.read()
}

```

Now, assume that the server crashes often leading to `conn` to be faulty. This would imply that the `write` and `read` methods in the `for` loop will not do anything and just return an `error`. Even if the server comes up again in the next few seconds the client's `for` loop code will not be able to communicate with the server since `conn` is faulty.

What I'm trying to achieve is: **Have the client to continue operating when the server comes back up again.** For this, I though of the following approach:

```
    func fixConnection(conn *net.Conn, reader **[]bufio.Reader, writer **[]bufio.Writer) net.Conn {

        for true {

            oneByte := make([] byte, 1, 1)
            reader := bufio.NewReader(*conn)
            _, err := reader.Read(oneByte)
            if err != nil {
                for true {
                    var tmpConn net.Conn
                    tmpConn, err = net.Dial("tcp", "127.0.0.1:3456")
                    if err == nil {
                        *conn = tmpConn
                        *reader = bufio.NewReader(*conn)
                        *writer = bufio.NewWriter(*conn)
                    }
                    time.Sleep(time.Millisecond * 100)
                }
            } else {
                reader.UnreadByte()
                time.Sleep(time.Millisecond * 500)
                continue
            }

        }
}

```

and then I just add one line in the client:

```
conn, _ := net.Dial("tcp", "127.0.0.1:3456")
reader := bufio.NewReader(conn)
writer := bufio.NewWriter(conn)

// new line
go fixConnection(&conn, &reader, &writer)

for true {
   writer.write(data)
   reader.read()
}

```

There is at lest one problem with my approach: `bufio` is not thread-safe so while `fixConnection` is changing the readers (writers) there might be an issue. Is there a way to solve this problem without using `sync.Mutex` before operating on readers/writers.

Also, is there a better way to solve my aforementioned problem. Namely, connecting again to the server when the server comes back up again? Note that the server can accept connections by multiple clients at any point in time.