Hamiltonian perturbation theory to fourth order in the gauge-coupling constant is applied to compute Luscher's effective hamiltonian for the zero-momentum gauge fields in a finite cubic volume in order to understand some apparent gauge dependence at the two-loop level. This allows us to confirm earlier results at a more rigorous level and to include new terms that mix coordinate and momentum operators, previously ignored. After that we live up to our promise to give some of the details for the derivation of the effective hamiltonian starting from Wilson's lattice action, which allowed a semi-analytic study of lattice artifacts. We also discuss some issues related to two-loop lattice perturbation theory. For easy reference the continuum effective hamiltonian and the lattice effective lagrangian, together with the specification of the boundary conditions and the numerical values of the coefficients, are summarized in a separate section.