check connection when reading and writing. Don't proceed if unable to connect