package net.yeputons.spbau.spring2016.torrent;

import net.yeputons.spbau.spring2016.torrent.protocol.Request;

import java.io.Closeable;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;

public class TorrentConnection implements Closeable {
    private final Socket socket;
    private final DataInputStream in;
    private final DataOutputStream out;

    public TorrentConnection(InetSocketAddress address) throws IOException {
        socket = new Socket(address.getAddress(), address.getPort());
        try {
            in = new DataInputStream(socket.getInputStream());
            out = new DataOutputStream(socket.getOutputStream());
        } catch (IOException e) {
            try {
                socket.close();
            } catch (IOException ignored) {
            }
            throw e;
        }
    }

    public synchronized <T> T makeRequest(Request<T> r) throws IOException {
        return r.makeRequest(in, out);
    }

    @Override
    public void close() {
        try {
            in.close();
        } catch (IOException ignored) {
        }
        try {
            out.close();
        } catch (IOException ignored) {
        }
        try {
            socket.close();
        } catch (IOException ignored) {
        }
    }
}

@@ -240,33 +240,22 @@
 ava.net.
-Inet
 Socket
-Address
 ;%0Aimport
@@ -270,16 +270,23 @@
 t.Socket
+Address
 ;%0A%0Apubli
@@ -479,12 +479,8 @@
 ion(
-Inet
 Sock
@@ -551,47 +551,41 @@
 ket(
-address.getAddress(), address.getPort()
+);%0A        socket.connect(address
 );%0A 

make it use SocketAddress, not InetSocketAddress