require "socket" attempts = 0 begin attempts += 1 TCPSocket.open("www.example.com", 80) { |socket| ... } rescue Exception => e if attempts < 3 then sleep 5 and retry else puts "Es reicht! #{e}" end end