public synchronized void handlePacket(Connection c, Packet p) {
    Address address = c.getAddress();
    System.out.println(p.toString());

    // silently ignore packets from a connection if we haven't received a version packet

    if (!c.hasReceivedVersion() && p.packetType() != PacketType.VERSION) {
      return;
    }

    switch (p.packetType()) {
      case VERSION:
        VersionPacket v = (VersionPacket) p;
        long ourVersion = ProtocolVersion.version(),
            theirVersion = v.getVersion(),
            negotiatedVersion;
        negotiatedVersion = theirVersion < ourVersion ? theirVersion : ourVersion;
        c.setVersion(negotiatedVersion);
        c.hasReceivedVersion(true);
        addressBook.justSeen(address);
        if (negotiatedVersion >= 209) {
          Packet verack = c.createPacket(PacketType.VERACK);
          c.sendPacket(verack);
        }
        break;
      case VERACK:
        c.hasRecievedVerack(true);
        addressBook.justSeen(address);
        break;
      case HEADERS:
        // primitive headers function
        HeadersPacket h = (HeadersPacket) p;
        if (h.headers().size() == 0) {
          break;
        }
        for (Block header : h.headers()) {
          try {
            blockChain.addBlock(header);

          } catch (InvalidBlockException e) {
            // TODO actually handle
            e.printStackTrace();
            continue;
          } catch (OrphanBlockException e) {
            // TODO actually handle
            e.printStackTrace();
            continue;
          } catch (BlockExistsException e) {
            // TODO actually handle
            e.printStackTrace();
            continue;
          }
        }
        GetHeadersPacket gh = (GetHeadersPacket) c.createPacket(PacketType.GETHEADERS);
        gh.startHashes().add(blockChain.topBlock().hash());
        c.sendPacket(gh);
    }
  }
 public synchronized void getInitialHeaders() {
   for (Connection c : connections) {
     GetHeadersPacket gh = (GetHeadersPacket) c.createPacket(PacketType.GETHEADERS);
     gh.startHashes().add(blockChain.topBlock().hash());
     c.sendPacket(gh);
   }
 }