title: Cannot connect to Minecraft Server
body: <p>I get this error when I try to connect to a Minecraft server using a slightly modified Minecraft client made from scratch in Java from here:
  Original Minecraft client Java code: <a href="https://stackoverflow.com/questions/30768091/java-sending-handshake-packets-to-minecraft-server">Java sending handshake packets to minecraft server</a>
  My modified version of the code above: <a href="https://pastebin.com/FmsBzKnm" rel="nofollow noreferrer">https://pastebin.com/FmsBzKnm</a>
  The error comes from somewhere here...</p>
  
  <pre><code>
          socket.close();
          Socket socket2 = new Socket();
  
          while (!socket2.isConnected()) {
              socket2.connect(host, 4000);
              Thread.sleep(500);
          }
  
          DataOutputStream out2 = new DataOutputStream(socket2.getOutputStream());
          DataInputStream in2 = new DataInputStream(socket2.getInputStream());
  
          int packetId2 = getHandshakeAndReturn(version, address, port, 2, out2, in2);
          byte[] test = new byte[readVarInt(in2)];
          in2.readFully(test);
          System.out.println(new String(test));
  </code></pre>
  
  <p>When I run this I get this error: </p>
  
  <pre><code>Internal Exception: io.netty.handler.codec.DecoderException: 
  java.lang.IndexOutOfBoundsException: 
  readerIndex(1) + length(1) exceeds writerIndex(1): 
  PooledUnsafeDirectByteBuf(ridx: 1, widx: 1, cap: 1)
  </code></pre>
  
  <p>Note: This error comes from the server which I connected to, not from my code itself. The server disconnects me with the error above.</p>