you can simply use the break command
it would go something like this:
if (communication is not working)
then (write to the log file AND break)
The break command may optionally take a parameter. A plain break terminates only the innermost loop in which it is embedded, but a break N breaks out of N levels of loop.
# break-levels.sh: Breaking out of loops.
# "break N" breaks out of N level loops.
for outerloop in 1 2 3 4 5
echo -n "Group $outerloop: "
for innerloop in 1 2 3 4 5
echo -n "$innerloop "
if [ "$innerloop" -eq 3 ]
break # Try break 2 to see what happens.
# ("Breaks" out of both inner and outer loops.)