Greetings! I am running a server capable of handling multiple connections. I have this small piece of code that is giving me problems when I try to send a number to the server

Java Code:
in=connection.getInputStream(); // opens the socket READ stream
		BufferedReader br= new BufferedReader(new InputStreamReader(in)); //recieve the line
		String line;
                 while( (line = br.readLine()) != null) {
                     	System.out.println(line); // we print the line
                        
                 }
                in.close();
                menuChoice=user_input.nextInt();
                System.out.println(menuChoice);
                DataOutputStream out;
                 out=new DataOutputStream(connection.getOutputStream());
                 out.writeInt(menuChoice);
                 out.flush();
                 out.close();
SERVER
Java Code:
@Override
  public void run()  {
      try {
          int menu;
          PrintWriter out = new PrintWriter(connection.getOutputStream(), true);
          out.print("Social System Menu \n1- Register \n2- Sign in \n");
          out.flush();
          out.close();
          DataInputStream in = new DataInputStream(connection.getInputStream());
          menu=in.readInt();        
          out = new PrintWriter(connection.getOutputStream(), true);
          switch (menu){            
             case 1:
                  out.print("You are going to register in the system");
              case 2:
                  out.print("You are going to log into the system");
                  logIn();
              default:
                  out.print("Wrong choice");
          }
          
    }
The problem is on the client when I try to send the INT with writeInt, it says the socket is closed. But I do not close the socket in the server, because I open it just a line before

Java Code:
 DataInputStream in = new DataInputStream(connection.getInputStream());
          menu=in.readInt();
The error netbeans give me is

Exception in thread "main" java.net.SocketException: Socket is closed
at java.net.Socket.getOutputStream(Socket.java:904)
at SocketClient.main(SocketClient.java:31)
Java Result: 1