Motivated by a peer-to-peer estimation algorithm in which adaptive weights are optimized to minimize the estimation error variance, we formulate and solve a novel nonconvex Lipschitz optimization problem that guarantees global stability of a large class of peer-to-peer consensus-based algorithms for wireless sensor network. Because of packet losses, the solution of this optimization problem cannot be achieved efficiently with either traditional centralized methods or distributed Lagrangian message passing. We prove that the optimal solution can be obtained by solving a set of nonlinear equations. A fast distributed algorithm, which requires only local computations, is presented for solving these equations. Analysis and computer simulations illustrate the algorithm and its application to various network topologies.