def check_previous_port(way,port)
begin
s = TCPSocket.open("localhost", port)
s.close
case way
when :reuse
puts "Port:#{port} has been opened. Trying to reuse the port..."
return nil
when :kill
puts "Port:#{port} has been opened. Trying to kill the port..."
client = XMLRPC::Client.new("localhost", "/RPC2", port)
client.call2("jb.exit")
return 0
when :abort
puts "Port:#{port} has been opened. Abort this program."
abort
end
rescue Errno::ECONNREFUSED => e
return 0
rescue => e
puts "Failed to deal with the opened port..."
abort
end
end