title: Connecting P2P over NAT?
body: <p>I started to explore the option of connecting with other using a p2p connection, so I coded a simple socket program in JAVA for android devices in which the users can share simple messages p2p (I didn't have any idea about NAT then). I got to know about NAT, so I now need to establish a TCP connection with another user which uses a server for discovery but payload is transferred p2p. I have also looked at XMPP(a very good and detailed explanation of how protocol works is <a href="http://ceit.uq.edu.au/content/how-xmpp-works-step-step" rel="noreferrer">here</a>) and UPnP but I dont know how to implement them.</p>
  
  <p>Another interesting question that arises is of BitTorrent because they can work on any device and even behind a NAT. I am not able to get any explanation of how BitTorrent works.</p>
  
  <p>I have researched a lot but I am stuck.</p>
  
  <p>My questions are:</p>
  
  <ol>
  <li>A detailed explanation of BitTorrent(like <a href="http://ceit.uq.edu.au/content/how-xmpp-works-step-step" rel="noreferrer">here</a>, not how torrents work) and how is it able to work around NAT ?</li>
  <li>Is there a way to make a NAT entry programmatically ?</li>
  <li>Is socket programming sufficient for p2p ?</li>
  <li>How difficult is it to create your own protocol and how can I build one ?</li>
  <li>If two devices D1 and D2 want to communicate p2p and they know each other's IP. D1 sends a request to D2 and that can't get through the D2's NAT, but there should be an entry created in D1's NAT. So when D2 tries to send something D1's NAT should discover an entry with D2's IP. Then why is the packet not allowed by it ?</li>
  </ol>