title: Why ServerSocket Object's accept method returns a Socket object with a different port
body: <p>I've searched stackoverflow for an answer to my question, but I can't get my answer from those threads. My question is why a ServeerSocket Object's <code>accept()</code> method returns a Socket object listening on a different port on the server machine, while the port that the server is listening to is another.</p>
  
  <p><strong>JAVA</strong> Code:</p>
  
  <pre><code>package chat.server;
  
  import java.io.*;
  import java.net.*;
  
  public class ServerApp {
      public String[] advices = {"Take smaller bites",
              "Go for the tight jeans. No they do NOT make you look fat.",
              "One word: inappropriate",
              "Just for today, be honest. Tell your boss what you *really* think",
              "You might want to rethink that haircut."};
      public ServerSocket serverSocket;
  
      public ServerApp()
      {
          try
          {
              serverSocket = new ServerSocket(4245);
              System.out.println("Server Started.");
          }catch (Exception exception)
          {
              exception.printStackTrace();
          }
      }
  
      public static void main(String[] args) {
          ServerApp server = new ServerApp();
          while(true)
          {
                  server.sendMessage();
          }
  
      }
  
      public void sendMessage()
      {
          String advice;
          try
          {
              Socket socket = serverSocket.accept();//here comes my question
              System.out.println(socket.getPort());
              PrintWriter printWriter = new PrintWriter(socket.getOutputStream());
  
              advice = getAdvice();
              printWriter.write(advice);
  
              printWriter.close();
              System.out.println(advice);
          }catch (Exception exception)
          {
              exception.printStackTrace();
          }
      }
  
      private String getAdvice() {
          int random = (int) (Math.random() * advices.length);
          return advices[random];
      }
  }
  </code></pre>
  
  <p>Isn't that normal that the server listens and answers the client from the same port number?</p>